Optional chaining
Optional chaining is a new feature in JavaScript that allows you to safely access nested properties or methods of an object, without worrying about whether intermediate properties or methods exist or not. Here's an example:
const person = {
name: 'John Doe',
address: {
street: '123 Main St',
city: 'New York',
state: 'NY'
}
};
console.log(person.address?.zipCode); // Output: undefined
In the code above, the person object has a nested address object. The optional chaining operator (?.) is used to safely access the zipCode property of the address object. If the address property does not exist, undefined will be returned.
No comments:
Post a Comment