This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // To convert the entire array to JSON: | |
| const myArray = ["💝", 5, true] | |
| console.log(myArray) | |
| // Output: Array(3) [ "💝", 5, true ] | |
| const myJSONString = JSON.stringify(myArray) | |
| console.log(myJSONString) | |
| // Output: ["💝",5,true] | |
| const parsedArray = JSON.parse(myJSONString) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Using && will prevent nonsense code from being executed. | |
| false && console.log(NoNsEnSe_CoDE) // nothing happens | |
| // You'll frequently see && used for type-checking. | |
| const bananas = "🍌🍌🍌🍌🍌" | |
| typeof bananas === "string" && console.log(bananas) // 🍌🍌🍌🍌🍌 | |
| // For example, you might be expecting an array, not a string. | |
| Array.isArray(bananas) && bananas.push("🍌") // nothing happens |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // With no type checks, the code seems to work fine. | |
| const user1 = { id: 1, name: "Erowyn" } | |
| const user2 = { id: 2, name: "Lavendula" } | |
| const user3 = { id: 3, name: "Fralia" } | |
| const userArray = [user1, user2, user3] | |
| for (const user of userArray) { | |
| console.log(`${user.id}: ${user.name}`) | |
| } | |
| // Output: | |
| // 1: Erowyn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // The typeof null is "object" in JavaScript. | |
| console.log(typeof null === "object") // true | |
| // Let's look at type checking a Date object. | |
| const date = new Date() | |
| console.log(typeof date) // "object" | |
| console.log(Object.prototype.toString.call(date)) // [object Date] | |
| console.log(date.constructor) // function Date() | |
| console.log(date.constructor.name) // "Date" | |
| console.log(date instanceof Date) // true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // && can be used to prevent errors, because the error won't be thrown. | |
| const nullBanana = null | |
| try { | |
| console.log(nullBanana.length) | |
| } catch (e) { | |
| console.log(e) | |
| } | |
| // Output: "TypeError: null has no properties." | |
| // Using && short-circuits the code, so the TypeError doesn't occur. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const object = { "🔑key🔑": "💸value💸" } | |
| console.log(typeof object) // "object" | |
| console.log(object !== null) // true | |
| console.log(object != null) // true | |
| console.log(typeof null) // "object" | |
| console.log(null !== null) // false | |
| console.log(null != null) // false | |
| console.log(typeof undefined) // "undefined" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Typical object declared with object literal notation | |
| const myObject = { zap: "⚡" } | |
| myObject.boom = "💣" | |
| // ES5 version using for...in loop | |
| for (const key in myObject) { | |
| if (myObject.hasOwnProperty(key)) { | |
| console.log(`myObject["${key}"] is ${myObject[key]}`) | |
| } | |
| } | |
| // Output: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Joining Two Strings with .concat() | |
| const stringOne = "🎅🤶" | |
| const stringTwo = "👩🚀👨🚀" | |
| // Use .concat() to join the strings: | |
| console.log(stringOne.concat(stringTwo)) | |
| // Output: 🎅🤶👩🚀👨🚀 | |
| // .concat() won't change the string: | |
| console.log(stringOne) // 🎅🤶 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Joining Two Arrays with ... Spread | |
| const array1 = ["🎅", "🤶"] | |
| const array2 = ["👩🚀", "👨🚀"] | |
| // Use ... spread to join the arrays: | |
| console.log([...array1, ...array2]) | |
| // Spread won't change either array: | |
| console.log(array1) | |
| console.log(array2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // We can get Set to compare objects' key-value pairs using JSON | |
| const duplicates = [{ hello: "🌞" }, { hello: "🌞" }] | |
| const objectsJSON = duplicates.map((object) => JSON.stringify(object)) | |
| const objectsJSONSet = new Set(objectsJSON) | |
| const uniqueJSONArray = Array.from(objectsJSONSet) | |
| // Equivalent to: (using the spread operator) | |
| // const uniqueJSONArray = [...objectsJSONSet] | |
| const uniqueObjectsByContent = uniqueJSONArray.map((string) => | |
| JSON.parse(string) | |
| ) |