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 string = "🍌,B,A,N,A,N,A,S" | |
| // .split() searches for a pattern | |
| const array = string.split(",") | |
| // Here, "," is the separator string | |
| console.log(array) | |
| // true | |
| // .join() is the same as .join(",") |
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 myObject = { "hello Bill": "👋" } | |
| myObject.helloWorld = "🌎" | |
| console.table(myObject) | |
| // (index) Value | |
| // hello Bill "👋" | |
| // helloWorld "🌎" | |
| try { | |
| // console.log(myObject.hello Bill) | |
| } catch (e) { | |
| console.log(e) |
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 date1 = new Date("3005-01-01 00:00") // Happy New Year! 🎉🎈🎊🥳 | |
| console.log(date1) | |
| // Output: Date Tue Jan 01 3005 00:00:00 GMT-0500 (Eastern Standard Time) | |
| // The date2 object will have a different object reference. | |
| const date2 = new Date("3005-01-01 00:00") // Happy New Year! 🎉🎈🎊🥳 | |
| // The date3 variable will have the same object reference as date1. | |
| const date3 = date1 |
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
| // Typically, you access key with . dot syntax. | |
| const emptyObject = {} | |
| const objectLiteral = { key: "value" } | |
| emptyObject.key = "value" | |
| // These two objects now have the same contents. | |
| // With emojis, you need to use square brackets. | |
| const emptyObject2 = new Object() | |
| const emojiObject = { "⭐": "💫" } | |
| emptyObject2["⭐"] = "💫" |
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 = { zero: "0️⃣" } | |
| const array = ["⛳"] | |
| console.log(object.zero) // 0️⃣ | |
| console.log(object["zero"]) // 0️⃣ | |
| console.log(array[0]) // ⛳ | |
| object["one"] = "1️⃣" | |
| object.two = "2️⃣" | |
| array[1] = "🏌️♂️" | |
| array[2] = "🎱" | |
| console.log(object.one) // 1️⃣ |
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 object1 = { "💐": "🌹" } | |
| Object.defineProperty(object1, "👟", { enumerable: false }) | |
| object1["👟"] = "🆒" | |
| object1[Symbol("🥞")] = "😋" | |
| console.log(Object.keys(object1).length) // 1 | |
| console.log(Object.keys({}).length) // 0 | |
| const object2 = { "💐": "🌹" } | |
| Object.defineProperty(object2, "👟", { enumerable: false }) | |
| object2["👟"] = "🆒" |
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
| // Read in old localStorage | |
| const pizza = JSON.parse(localStorage.getItem("pizza")) | |
| if (!pizza) { | |
| console.log("No pizza was found in localStorage.") | |
| console.log("Let's save {pizza: '🍕'} to localStorage.") | |
| console.log("Your slice of pizza will be waiting for you!") | |
| // You can't save the object directly to localStorage: | |
| // window.localStorage.setItem('pizza', {pizza: "🍕"}) | |
| // Instead, call JSON.stringify() on the object first: | |
| const freshPizza = JSON.stringify({ pizza: "🍕" }) |
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
| // Every symbol created with Symbol() is unique. | |
| console.log(Symbol() === Symbol()) // false | |
| console.log(Symbol("✨") === Symbol("✨")) // false | |
| // Calling Symbol.for() makes a global symbol. | |
| console.log(Symbol.for("✨") === Symbol.for("✨")) // true | |
| // You can check for a symbol using typeof. | |
| console.log(typeof Symbol()) // "symbol" |
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 myObject = { key: "🍫" } | |
| myObject.key2 = "🍬" | |
| myObject.key3 = "🍭" | |
| console.log(delete myObject.key) | |
| myObject.key2 = undefined | |
| console.table(Object.entries(myObject)) | |
| // (index) key value | |
| // 0 key2 undefined | |
| // 1 key3 🍭 |
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 myArray = [2, 1, "🎊", "✨", true, false, false, true, "✨", "🎊", 1, 2] | |
| const mySet = new Set(myArray) | |
| const uniqueValues = Array.from(mySet) | |
| console.log(uniqueValues) | |
| // Output: Array(6) [2, 1, "🎊", "✨", true, false] | |
| // Using the ... spread operator is the same as Array.from(): | |
| console.log([...mySet]) | |
| // Output: Array(6) [2, 1, "🎊", "✨", true, false] |