Skip to main content

JS FUNCTION

FUNCTION

function add (a,b)
{var c = a+b;
return c}
var ans = add(4,5)
console.log(ans)

ANAYMOUS FUNCTION

var add = function (a,b)
{var c = a+b;
return c}
var res = add(5,5)
console.log(res)


IIFE - Immediately Invoked Functional Expression

(function (a,b)
{var c = a+b;
console.log c})(6,8)


ARROW FUNCTION


var add =(a,b=>return a+b;
var res = add(5,5)
console.log(res)


function add (a,b)
{var c = a+b;
return c}

var add =function(a,b)
{var c = a+b;
return c}

(function(a,b)
{var c = a+b;
return c})(4.5)

var add =(a,b=>return a+b;

PARAMETER & ARGUMENTS


function add (a,b,c)
{return a+b+c}

add(1,2,3//6

add(1,2,3,4,5//6  it takes only the first three arguments

add(1,2)//NaN the missed argument is taken as "undefined"

DEFAULT PARAMETERS

function add (a=10,b,c)
{return a+b+c}

add(1,2,3//6

add(1,2)//NaN the missed argument is taken as "undefined"

function add (a,b,c=10)
{return a+b+c}

add(1,2,3//6

add(1,2)//13



Comments

Popular posts from this blog

  You are provided with the radius of a circle "A". Find the length of its circumference. Note: In case the output is coming in decimal, roundoff to 2nd decimal place. In case the input is a negative number, print "Error". function   coc () { var   r = document . getElementById ( "num1" ). value ; var   pi = 3.1415 ; var   Circumference =( 2 * pi * r ); if  ( r >= 0 ) console . log ( Circumference ); else console . log ( "Error" )} 
The area of an equilateral triangle is ¼(√3a 2 ) where "a" represents a side of the triangle. You are provided with the side "a". Find the area of the equilateral triangle. function   sqrt () { var   a = document . getElementById ( "a" ). value ; b =  Math . sqrt ( 3 ); c =( 1 / 4 )* b * a * a ; console . log (  "Area =" + c )}

HOISTING

  HOISTING Variables” and “ function” declarations are moved to the top of their scope before code execution. Variables are hosted and not their values Only the normal function will be hoisted not the anonymous,IIFE and Arrow Function will be hoisted Properties are not hoisted PROPERTY  Any variable declared globally  become property of window variable declared without keyword variable declared with keyword var except function scope var console . log ( a )   // Error a not defined console . log ( a );  // undefined --> var a is hoisted not value var   a  =  20 ; console . log ( a , b )  // Error --->property b can't hoist var   a  =  20 ; b  =  40 ; var   a  = add ( 2 , 5 )  // Error console . log ( a ) var   a  = add ( 2 , 5 )  // 7 function add hoist console . ...