Default values in destructuring assignment allow you to assign a default value to a variable if the value being destructured is undefined
. This is useful when destructuring objects or arrays with missing values.
Example with array:
let [x = 10, y = 20] = [5];
console.log(x, y); // Output: 5 20
Example with object:
let { name = 'Guest', age = 25 } = { name: 'John' };
console.log(name, age); // Output: John 25
Tags: intermediate, JavaScript, Destructuring
URL: https://www.tiktok.com/@jsmentoring/photo/7471727005394930966