You probably confused by the difference between null and undefined, For example:
const { dogName = 'snickers' } = { dogName: undefined }
console.log(dogName) // what will it be? 'snickers'!
const { dogName = 'snickers' } = { dogName: null }
console.log(dogName) // what will it be? null!
const { dogName = 'snickers' } = { dogName: false }
console.log(dogName) // what will it be? false!
const { dogName = 'snickers' } = { dogName: 0 }
console.log(dogName) // what will it be? 0!
const { dogName = 'snickers' } = { }
console.log(dogName) // what will it be? 'snickers'!
Taken from: http://wesbos.com/destructuring-default-values/
Answer from Shai Katz on Stack Overflow Top answer 1 of 2
104
You probably confused by the difference between null and undefined, For example:
const { dogName = 'snickers' } = { dogName: undefined }
console.log(dogName) // what will it be? 'snickers'!
const { dogName = 'snickers' } = { dogName: null }
console.log(dogName) // what will it be? null!
const { dogName = 'snickers' } = { dogName: false }
console.log(dogName) // what will it be? false!
const { dogName = 'snickers' } = { dogName: 0 }
console.log(dogName) // what will it be? 0!
const { dogName = 'snickers' } = { }
console.log(dogName) // what will it be? 'snickers'!
Taken from: http://wesbos.com/destructuring-default-values/
2 of 2
9
No, it is not compatibility problem.
Default value will works if there's no value, meaning that it will work if it is undefined. From your example, you're assigning null to prop2, and null is a defined value.
So it will work if your prop2 is not defined or assigned with undefined
let foo = {
prop1: 'hello!',
prop2: undefined
}
const { prop1 = {} } = foo
const { prop2 = {} } = foo
console.log(prop1) // hello!
console.log(prop2) // {}
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Destructuring
Destructuring - JavaScript - MDN Web Docs - Mozilla
... Objects passed into function ... have the same name or a different name than the original property, and to assign default values for the case when the original object does not define the property....
TutorialsPoint
tutorialspoint.com › article › how-to-set-default-values-when-destructuring-an-object-in-javascript
How to set default values when destructuring an object in JavaScript?
For example, we have added the property3 variable in the destructuring object, but if the object doesn't contain the property3, destructuring sets the undefined value to the property3 variable.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Destructuring_assignment
Destructuring - JavaScript | MDN
You can only use assignment patterns as the left-hand side of the assignment operator. You cannot use them with compound assignment operators such as += or *=. Each destructured property can have a default value. The default value is used when the property is not present, or has value undefined.
Codementor
codementor.io › community › destructuring with default values in javascript
Destructuring with default values in JavaScript | Codementor
March 26, 2024 - // We get an error like TypeError: ... value. const {exportData = [], downloadData = []} = templateData || {}; /* we can then loop through the `exportData` array * using .map() or any of the es6 functions to iterate over an array. * without crashing the app */ Now, to the main part. Providing default values in destructuring assignment works only if there is no variable or its value is set to undefined...
Thomas Step
thomasstep.com › blog › destructuring-objects-in-javascript
Destructuring Objects in Javascript - Thomas Step
August 12, 2021 - Let’s say you have an object that is conditionally populated with properties; attempting to destructure those properties could be a hit or a miss. The example above shows what happens if we do not provide a default value (variable is undefined), how to provide a default value (other = 'defaultOtherString'), and how to provide a default value to a renamed property (numberValue: randomNumber = 3).
Webdevtutor
webdevtutor.net › blog › javascript-destructuring-default-value
Exploring JavaScript Destructuring Default Values
This can help prevent errors and provide fallback values when working with potentially missing properties. To assign default values during destructuring, you can simply specify the default value using the assignment operator (=) within the destructuring syntax.
Zaiste
zaiste.net › posts › javascript-destructuring-assignment-default-values
JavaScript · Destructuring Assignment with Default Values · Zaiste Programming
August 3, 2018 - Any other value, including null, false and 0, bypasses the default values in the destructuring statement. const dummy = { name: undefined } const { name = 'Basia' } = dummy; console.log(name) ... You can combine default values with renaming in the destructuring assignment.
Fridoverweij
library.fridoverweij.com › docs › jstutorial › object_destructuring.html
JavaScript Tutorial – Object destructuring
April 29, 2024 - Each destructured element or property can be appointed a default value. The default value gets assigned only when the element or property is not present or when it has value undefined.
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript object destructuring
JavaScript Object Destructuring
December 17, 2023 - let person = { firstName: 'John', lastName: 'Doe', currentAge: 28 }; let { firstName, lastName, middleName = '', currentAge: age = 18 } = person; console.log(middleName); // '' console.log(age); // 28Code language: JavaScript (javascript) In this example, we assign an empty string to the middleName variable when the person object doesn’t have the middleName property. Also, we assign the currentAge property to the age variable with the default value of 18.