Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 845 Bytes

File metadata and controls

32 lines (25 loc) · 845 Bytes

Default Function Parameters

In case of:

function sayHi (name, greeting) {
  return `${greeting} ${name}`;}
  
sayHi('Diana', 'Hi');//Hi Diana
sayHu();//Uncaught ReferenceError: sayHu is not defined at ...

You can write

function sayHi (name, greeting) {
name = (name !== undefined)? name : 'Islam';
greeting = (greeting !== undefined)? greeting : 'Hi'
  return `${greeting} ${name}`;}
  
sayHi();// Hi Diana
sayHi('Monica', 'Hello');//Hello Monica

So you can write this instead:

function sayHi (name = 'Diana', greeting = 'Hi') {
  return `${greeting} ${name}`;}
  
sayHi();// Hi Diana
sayHi('Monica', 'Hello');//Hello Monica

This is default function parameters= Assigning a default values for function parameters in case no values are assigned when invoking the function.