Skip to content

Instantly share code, notes, and snippets.

View DoctorDerek's full-sized avatar
☀️
Read my blog https://DoctorDerek.medium.com

Dr. Derek Austin DoctorDerek

☀️
Read my blog https://DoctorDerek.medium.com
View GitHub Profile
@DoctorDerek
DoctorDerek / How To Turn an Array Into a String Without Commas in JavaScript.js
Last active January 27, 2021 18:28
How To Turn an Array Into a String Without Commas in JavaScript https://medium.com/p/241598bb054b
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(",")
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)
@DoctorDerek
DoctorDerek / 1 - Does each Date have a unique "object reference" in memory?.js
Created December 24, 2020 19:31
How to Find Unique Dates in an Array in JavaScript
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
@DoctorDerek
DoctorDerek / What Is the Object Literal Syntax in JavaScript?.js
Last active December 23, 2020 20:41
What Is the Object Literal Syntax in JavaScript?
// 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["⭐"] = "💫"
@DoctorDerek
DoctorDerek / What Are the Object Property Accessors in JavaScript?.js
Created December 23, 2020 20:04
What Are the Object Property Accessors in JavaScript?
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️⃣
@DoctorDerek
DoctorDerek / How to Check If a JavaScript Object Is Empty of Properties.js
Created December 23, 2020 19:17
How to Check If a JavaScript Object Is Empty of Properties
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["👟"] = "🆒"
@DoctorDerek
DoctorDerek / Can We Store JavaScript Objects in LocalStorage.js
Created December 19, 2020 23:16
Can We Store JavaScript Objects in LocalStorage
// 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: "🍕" })
@DoctorDerek
DoctorDerek / How to Check for a Symbol in JavaScript.js
Last active April 25, 2023 02:25
How to Check for a Symbol in JavaScript
// 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"
@DoctorDerek
DoctorDerek / How to Remove a Key from an Object in JavaScript1.js
Created December 18, 2020 20:52
How to Remove a Key from an Object in JavaScript
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 🍭
@DoctorDerek
DoctorDerek / Get Unique Values From a JavaScript Array Using Set (ES6).js
Created December 18, 2020 00:29
Get Unique Values From a JavaScript Array Using Set (ES6)
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]