Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 860 Bytes

default-arguments.md

File metadata and controls

33 lines (24 loc) · 860 Bytes

Default arguments

If a function has multple arguments but only one is passed, it will return undefined to get around this we can use default argument values

const someFunc = (x, y) => {
    return x * y;
};

console.log(someFunc(10, 10)); // returns 100
console.log(someFunc(10)); // returns undefined (NaN) due to no 2nd argument

To get around the above we can assign a default value to an argument like so.

const someFunc = (x, y = 10) => {
    return x * y;
};

console.log(someFunc(10, 10)); // returns 100
console.log(someFunc(10)); // returns 100 as we assigned a default value of 10

You can also pass undefined as an argument if you werent to pass the first argument

const someFunc = (x = 10, y = 10) => {
    return x * y;
};

console.log(someFunc(undefined, 10)); // returns 100 still