Function:
Function is a sub program to perform specific task.It is a reusable code that calls anywhere in program.
"function" is a keyword to define function in JS.
That returns a value to the called function.
Syntax:
function fun_name(args)
{
statements;
return value;
}
Task: To display name in bold and blue color.
Code:
Output:
Before clicking button:
After clicking button:
Function with parameters:
Code:
Output:
Function with invokes and return value:
Syntax:function_name() (or) result=function_name()
Code:
Output:
Local and Global Variables in JS:
1)A variable that is declared inside a function definition is called local and has scope to that function only.
2)A variable that is declared outside of a function definition is called a global variable and its scope is throughout your program means its value is accessible and modifiable throughout your program.
3)Syntax:
<script>
var name="uma"; //global variable
function showName() {
var name="Umamaheswari"; //local variable
console.log(name);
}
</script>
4) A variable with in function with var keyword will act as local variable and without var keyword will act as global keyword.