d
Amit DhamuSoftware Engineer
 

Default Parameter in Function

1 minute read 00000 views

In pre-ES6 Javascript, we can't assign a default value to a function parameter in it's parenthesis.

Looking at the function below, we have two arguments. We want the age parameter to be optional. To do this:

function myFunction(name, age) {
  var age = typeof age === 'undefined' ? null : age
}