javascript optional parameter code example

Example 1: javascript optional parameters

const myFunction = (str, id=0) => {
	console.log(str, id)
} 
myFunction("Hello Grepper")
// OUTPUT : "Hello Grepper 0"

myFunction("HG", 10)
// OUTPUT : "HG 10"

Example 2: optional function parameter javascript

function check(a, b = 0) {
  document.write("Value of a is: " + a + 
                 " Value of b is: " + b + 
                 "<br>");
}
check(9, 10);
check(1);

Example 3: variable defaults javascript es6

let variableToSet = checkedValue || defaultValue;