// destructuring with default value function someFunction({ a = 1 } = {}) { // a does not exist in the object, always defaults to 1 console.log(a); } someFunction(); // 1
OR
const value = fetchValue(); // can be undefined const obj = { variables: { value: value || 1 // default value is 1 } }; function someFunction(obj) { console.log(obj.variables.value); } someFunction(obj); // 1
Caveat
You cant do this:
const value = fetchValue(); // can be undefined function someFunction({ variables: { value: value || 1 } }) { console.log(obj.variables.value); }
When you pass a literal structure in the argument, it will be parsed as a destructuring assignment and will cause syntax error when handling a default value with a ||
operator.
arguments
keywordArray-like object accessible inside functions that contain the values of the arguments passed to that function.
function someFunction(a, b, c) { console.log(arguments); } someFunction(1, 2, 3); // { 0: 1, 1: 2, 2: 3 }