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 articleInternally, there is no type called “array” in JavaScript. When used on an array, typeof
returns "object"
instead.
To check if something is an array, use Array.isArray()
on the supposed array instead.
// `typeof` an array returns “object” because JS has no type called “array”.
typeof ["a", "b", "c"] // ⇒ "object"
// The array is treated like this equivalent object.
typeof { 0: "a", 1: "b", 2: "c" } // ⇒ "object"
// Use `Array.isArray` instead of `typeof` to test if something is an array.
Array.isArray(["a", "b", "c"]) // ⇒ true
Array.isArray({ 0: "a", 1: "b", 2: "c" }) // ⇒ false
// `typeof` an array returns “object” because JS has no type called “array”.
typeof ["a", "b", "c"] // ⇒ "object"
// The array is treated like this equivalent object.
typeof { 0: "a", 1: "b", 2: "c" } // ⇒ "object"
// Use `Array.isArray` instead of `typeof` to test if something is an array.
Array.isArray(["a", "b", "c"]) // ⇒ true
Array.isArray({ 0: "a", 1: "b", 2: "c" }) // ⇒ false
// `typeof` an array returns “object” because JS has no type called “array”.
typeof ["a", "b", "c"] // ⇒ "object"
// The array is treated like this equivalent object.
typeof { 0: "a", 1: "b", 2: "c" } // ⇒ "object"
// Use `Array.isArray` instead of `typeof` to test if something is an array.
Array.isArray(["a", "b", "c"]) // ⇒ true
Array.isArray({ 0: "a", 1: "b", 2: "c" }) // ⇒ false
// `typeof` an array returns “object” because JS has no type called “array”.
typeof ["a", "b", "c"] // ⇒ "object"
// The array is treated like this equivalent object.
typeof { 0: "a", 1: "b", 2: "c" } // ⇒ "object"
// Use `Array.isArray` instead of `typeof` to test if something is an array.
Array.isArray(["a", "b", "c"]) // ⇒ true
Array.isArray({ 0: "a", 1: "b", 2: "c" }) // ⇒ false
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 articleWith JavaScript’s built-in formatter for relative timestamps, we can build strings like “2 months from now” without third party libraries.
Read full articleTo asynchronously fetch data in a React component using hooks, we can define and then call an asynchronous function inside of useEffect.
Read full articleWe can pass an array to JavaScript’s sort function to sort an array of objects by one of their properties.
Read full articleWhen naming variables that hold Boolean values, using a prefix that indicates as much helps with readability.
Read full articleSince array methods like filter and map return an array themselves, we can chain them one after the other.
Read full article