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 »



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.