Comparing to null with double-equals
We usually want to compare two values using three equal signs in JavaScript. There is one exception where two equal signs are more practical.
Read full articleWe can get nested values from objects by chaining their keys. We run into an error when we try to get a value from something that does not exist. To avoid that, we had to check if any value along the chain was undefined
.
With optional chaining, these calls return those undefined
values instead of throwing errors.
// this person’s address is nested in the outer object as another object
const mario = {
occupation: "Plumber",
address: {
street: "Rainbow Road",
zip: "81664"
}
}
// we can get the full address, or drill into it to get the street
mario.address // ⇒ { street: "Rainbow Road": zip: "81664" }
mario.address.street // ⇒ "Rainbow Road"
// we don’t have a nested address for this person
const toad = {
occupation: "Explorer"
}
// if we try to get the street, JavaScript will throw an error
toad.address // ⇒ undefined
toad.address.street // TypeError: undefined is not an object
// the ?. operator returns `undefined` if it hits any undefined value
toad?.address // ⇒ undefined (same as without the questionmark)
toad?.address?.street // ⇒ undefined (no error!)
// this person’s address is nested in the outer object as another object
const mario = {
occupation: "Plumber",
address: {
street: "Rainbow Road",
zip: "81664"
}
}
// we can get the full address, or drill into it to get the street
mario.address // ⇒ { street: "Rainbow Road": zip: "81664" }
mario.address.street // ⇒ "Rainbow Road"
// we don’t have a nested address for this person
const toad = {
occupation: "Explorer"
}
// if we try to get the street, JavaScript will throw an error
toad.address // ⇒ undefined
toad.address.street // TypeError: undefined is not an object
// the ?. operator returns `undefined` if it hits any undefined value
toad?.address // ⇒ undefined (same as without the questionmark)
toad?.address?.street // ⇒ undefined (no error!)
// this person’s address is nested in the outer object as another object
const mario = {
occupation: "Plumber",
address: {
street: "Rainbow Road",
zip: "81664"
}
}
// we can get the full address, or drill into it to get the street
mario.address // ⇒ { street: "Rainbow Road": zip: "81664" }
mario.address.street // ⇒ "Rainbow Road"
// we don’t have a nested address for this person
const toad = {
occupation: "Explorer"
}
// if we try to get the street, JavaScript will throw an error
toad.address // ⇒ undefined
toad.address.street // TypeError: undefined is not an object
// the ?. operator returns `undefined` if it hits any undefined value
toad?.address // ⇒ undefined (same as without the questionmark)
toad?.address?.street // ⇒ undefined (no error!)
// this person’s address is nested in the outer object as another object
const mario = {
occupation: "Plumber",
address: {
street: "Rainbow Road",
zip: "81664"
}
}
// we can get the full address, or drill into it to get the street
mario.address // ⇒ { street: "Rainbow Road": zip: "81664" }
mario.address.street // ⇒ "Rainbow Road"
// we don’t have a nested address for this person
const toad = {
occupation: "Explorer"
}
// if we try to get the street, JavaScript will throw an error
toad.address // ⇒ undefined
toad.address.street // TypeError: undefined is not an object
// the ?. operator returns `undefined` if it hits any undefined value
toad?.address // ⇒ undefined (same as without the questionmark)
toad?.address?.street // ⇒ undefined (no error!)
We usually want to compare two values using three equal signs in JavaScript. There is one exception where two equal signs are more practical.
Read full articleIf an arrow function immediately returns a value, we don’t have to write the return keyword. The function will still implicitly return that value.
Read full articleFinding an object with a specific property is slow in large arrays. We can speed this operation up by transforming the array to a lookup object.
Read full articleAre you calling the same function many times with near-identical parameters? Hide that repetition in a higher-order function for more readability.
Read full articleWant to create annoying memes but are too lazy to hit the shift key every other letter? Don’t worry, I got you.
Read full articleBy wrapping code that can fail in try-catch, we can react to and recover from any errors that happen.
Read full article