Javascript functions


JavaScript functions:
A function is a block of code, meant to perform a specific task.
Whenever we want to perform some specific task, we create a function.

Creating a function:
There are many ways in which we can create a function in JavaScript
1.Using function declaration
2.Using function expression
3.Using function constructor
4.Using IIFE (Immediately Invokable Function Expression)

1. Function declaration:
A function defined with function declaration must begin with function keyword

Syntax: /* function definition */
function functionName( [param1,param2, …. paramN] ) // function header
// by default a function returns undefined value
[return returningValue;]

Ex: a function without parameters, without returning value
function wishHi()
document.write( “Hi” , ”<br/>” );

Note: Functions get execute only when we call them

Syntax: /* function call */
functionName( [ param1, param2 …. paramN ] );

wishHi(); // Hi

Note: Why Functions? Functions are created
to divide a larger problem into smaller tasks or modules (manageability).
for reusability (I.e. once a function is defined, it can be used multiple times)
for abstraction (I.e. hiding the way how tasks are done) (creating libraries).
wishHi(); // Hi
wishHi(); // Hi
wishHi(); // Hi

Interview Questions: