JavaScript | Some of the examples of user-defined functions

User-defined functions examples in JavaScript: Here, we are writing some of the examples of User-defined functions in JavaScript.
Submitted by Pankaj Singh, on October 10, 2018

1) Design a function, print message and assign the function to a variable and print it like a function

<html lang="en">
<head>
    <script>
        function hello(){
            document.write("Hello <Br />")
        }

        hello();
        a=hello;
        a();
    </script>
</head>
<body>
    
</body>
</html>

Output

Hello 
Hello 

2) Call a function and then define it

<html lang="en">
<head>
    <script>
        hello();
        function hello(){
            document.write("Hello <Br />")
        }
    </script>
</head>
<body>
    
</body>
</html>

Output

Hello

3) Define a function and call it in the body section

<html lang="en">
<head>
    <script>
        function hello(){
            document.write("Hello <Br />")
        }
    </script>
</head>
<body>
    <script>
        hello();
    </script>
</body>
</html>

Output

Hello

4) Call a function in head section and define it in the body section

<html lang="en">
<head>
    <script>
        hello(); 
    </script>
</head>
<body>
    <script>
        function hello(){
            document.write("Hello <Br />")
        }
    </script>
</body>
</html>

Output

None: Function will not call

5) Call a function and define it in the body section

<html lang="en">
<head>
    <script>
         
    </script>
</head>
<body>
    <script>
        hello();
        function hello(){
            document.write("Hello <Br />")
        }
    </script>
</body>
</html>

Output

Hello

JavaScript Examples »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.