What is a function?
Function is defined as a set of systematic procedures followed to attain a particular goal.
What is a function in a programming language ?
Functions in programming language is considered as block of standard codes which can be reused by calling multiple times inside the program which provides the desired output, without re-typing the code again and again.
What is a function in javascript ?
In javascript function is a keyword, defined as a block of code that can be called once or multiple times when it is called inside the program to get the desired output.
What are the types of functions in javascript ?
There are two types of functions in javascript, they are
1. User defined function
2. Javascript In-Built function
Javascript interview questions basic !!!
Click to Learn More about – Javascript online learning
1. User defined function :
Javascript allows us to create our functions inside the program, this is a user defined function.
These functions can be written by the developer/user inside the program to get the desired output.
2. JavaScript In-Built function
Javascript has some in-built function, which can be called inside the program.
We cannot modify these functions, as its protocol is already defined.
These inbuilt functions have their own logical codes which is present in Javascript libraries.
Syntax :
function FunctionName ([arg1, arg2, arg3 .... argn]){
// Piece of code to be executed
}
What are the rules to be followed for a function ?
- Function should begin with the keyword function followed by {function name}
2. User defined function names should be unique,
3. Functional parameters should be enclosed within parenthesis and separated by commas,
4. The statements inside the body of the function enclosed within curly braces {}.
1. Simple function
Example :
<!DOCTYPE html>
<html>
<head>
<title>Our title</title>
<script type="text/javascript">
function doSomthing(){
document.write("Hi this is test function");
}
</script>
</head>
<body></body>
</html>
2. Calling a function
Example :
<!DOCTYPE html>
<html>
<head>
<title>Our title</title>
<script type="text/javascript">
function doSomthing(){
document.write("Hi this is test function");
}
</script>
</head>
<body>
<button type="button" onClick="doSomthing();">Click Me</button>
</body>
</html>
Output :


3. Function with Params
Example :
<!DOCTYPE html>
<html>
<head>
<title>Our title</title>
<script type="text/javascript">
function doSomthing(a,b){
document.write("The value of a = " + a + "<br/>");
document.write("The value of b = " + b + "<br/>");
var c = a+b;
document.write("The addition value of a and b = " + c + "<br/>");
}
</script>
</head>
<body>
<button type="button" onClick="doSomthing(3,6);">Click Me</button>
</body>
</html>
Output :
The value of a = 3
The value of b = 6
The addition value of a and b = 9
4. Functions with return value
Example :
<!DOCTYPE html>
<html>
<head>
<title>Our title</title>
<script type="text/javascript">
function doSomthing(a,b){
document.write("The value of a = " + a + "<br/>");
document.write("The value of b = " + b+ "<br/>");
return a+b;
}
document.write("The addition value of a and b = " + doSomthing(2,3));
</script>
</head>
<body>
</body>
</html>
Output :
The value of a = 2
The value of b = 3
The addition value of a and b = 5
5. Functions accessing Global/Local variables.
Example :
<!DOCTYPE html>
<html>
<head>
<title>Our title</title>
<script type="text/javascript">
var a = 100; // Global Variable
function doSomthing(a){
var b = 200; // Local Variable
document.write("The value of a = " + a + "<br/>");
document.write("The value of b = " + b+ "<br/>");
return a+b;
}
document.write("The addition value of a and b = " + doSomthing(a));
</script>
</head>
<body>
</body>
</html>
Output :
The value of a = 100
The value of b = 200
The addition value of a and b = 300
6. Functions as Variable Values
Example :
<!DOCTYPE html>
<html>
<head>
<title>Our title</title>
<script type="text/javascript">
var a = 100; // Global Variable
function doSomthing(a){
var b = 200; // Local Variable
document.write("The value of a = " + a + "<br/>");
document.write("The value of b = " + b+ "<br/>");
return a+b;
}
var x = doSomthing(a); // Function assigned to a variable
document.write("The addition value of a and b = " + x + "<br/>");
</script>
</head>
<body>
</body>
</html>
Output :
The value of a = 100
The value of b = 200
The addition value of a and b = 300
7. Function called from another function
Example :
<!DOCTYPE html>
<html>
<head>
<title>Our title</title>
<script type="text/javascript">
var a = 100; // Global Variable
function doSomthing(a){
var b = 200; // Local Variable
document.write("The value of a = " + a + "<br/>");
document.write("The value of b = " + b+ "<br/>");
return a+b;
}
function FuncOne(){
return("The addition value of a and b = " + doSomthing(a) + "<br/>");
}
document.write(FuncOne());
</script>
</head>
<body>
</body>
</html>
Output :
The value of a = 100
The value of b = 200
The addition value of a and b = 300