A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Default Arguments ES6

Default Arguments

Default Arguments

  • It allows to set default values for function Arguments, if no value or if undefined is passed.

Example what happen when no parameter is passed to a function-





function addTest(x,y)
{
    return x + y;  
}
addTest()

Output:-
NaN


 

Example:-





function test(x, y = 1)
{
  return x * y;
}

console.log(test(10, 2));

console.log(test(5));

Output:-
20
5


 

Now Example to pass arguments to function using ES6:-





function Add(x, y)
{
 y = (typeof y !== 'undefined') ?  y : 1
 return x * y
}

Add(5, 2) 
Add(5)    

Output:-
5


 












LEARN TUTORIALS

.

.