Home »
JavaScript Examples
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
Example 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
Example 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
Example 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
Example 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
Example 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 »