# functions define and calling a function 1) //defining a function function <function-name>(){ // code to be executed }; //calling a function <function-name>(); Example: Define and Call a Function //defining a function function ShowMessage() { alert("Hello World!"); } ShowMessage(); //calling a function 2) A function can return another function in JavaScript. Example: Function Returning a Function function multiple(x) { function fn(y) { return x * y; } return fn; } var triple = multiple(3); triple(2); // returns 6 triple(3); // returns 9 3) Type of function:---- 1. Regular function // Function declaration: starts with "function" function multiply(x, y){ return x * y; } // Function expression: starts with "var,let,const" JavaScript allows us to assign a function to a variable and then use that variable as a function. It is called function expression. var add = function sum(val1, val2) { return val1 + val2; }; var result1 = add(10,20); An easy to remember rule: the function declaration in a statement always starts with the keyword function. Otherwise it’s a function expression 2. Anonymous Function a function without any name.This unnamed function is called anonymous function. Anonymous function must be assigned to a variable. example:-- var showMessage = function (){ alert("Hello World!"); }; showMessage(); 3.Nested Functions Inner function can access variables and parameters of outer function. However, outer function cannot access variables defined inside inner functions. example:--- function ShowMessage(firstName) { function SayHello() { alert("Hello " + firstName); } return SayHello(); } ShowMessage("Steve"); 4. Arrow Function OR Function Shorthand methods: Example:-- a) var double = (value) => {return value * 2;} //multiple line then use return b) var double = (value) => value * 2; b) var double = (value) => value * 2; //for use single line 4. Generator Function: Example:---- function * generatorFunction() { yield '1'; yield '2'; yield '3'; yield '4'; yield '5'; } var generatorObject = generatorFunction(); console.log(generatorObject.next().value); console.log(generatorObject.next().value); console.log(generatorObject.next().value); 5. Function with: new Function var sum = new Function('a', 'b', 'return a + b'); var add = function(a, b){ return a + b; }