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
"editor.wordWrap": "wordWrapColumn",
"workbench.colorTheme": "Monokai Vibrant",
"workbench.iconTheme": "vscode-icons",
"rainbowTags.allowEverywhere": true,
"emmet.triggerExpansionOnTab": true,
"js/ts.implicitProjectConfig.checkJs": true
@DoctorDerek
DoctorDerek / Using Array.isArray to check for an Array.js
Created November 17, 2020 19:45
Using Array.isArray to check for an Array
const myObject = {}
console.log(typeof myObject) // "object"
console.log(Array.isArray(myObject)) // false
const myArray = []
console.log(typeof myArray) // "object"
console.log(Array.isArray(myArray)) // true
const myNull = null
console.log(typeof myNull) // "object"
@DoctorDerek
DoctorDerek / Using Object.prototype.toString.call() to check for an Array.js
Created November 17, 2020 19:51
Using Object.prototype.toString.call() to check for an Array
const isArray = (maybeArray) =>
Object.prototype.toString.call(maybeArray).slice(8, -1) === "Array"
const myObject = {}
console.log(Object.prototype.toString.call(myObject)) // [object Object]
console.log(Object.prototype.toString.call(myObject).slice(8, -1)) // Object
console.log(isArray(myObject)) // false
const myArray = []
console.log(Object.prototype.toString.call(myArray)) // [object Array]
@DoctorDerek
DoctorDerek / Checking for an array using the .constructor property.js
Created November 17, 2020 19:52
Checking for an array using the .constructor property
const isArray = (maybe) => maybe.constructor.name === "Array"
const myObject = {}
console.log(myObject.constructor) // function Object()
console.log(myObject.constructor.name) // Object
console.log(isArray(myObject)) // false
const myArray = []
console.log(myArray.constructor) // function Array()
console.log(myArray.constructor.name) // Array
// NOTE: instanceof won't work inside iframes
const myObject = {}
console.log(myObject instanceof Object) // true
console.log(myObject instanceof Array) // false
const myArray = []
console.log(myArray instanceof Object) // true
console.log(myArray instanceof Array) // true
const myNull = null
@DoctorDerek
DoctorDerek / Checking for null and an array using the && .constructor one-liner.js
Created November 17, 2020 19:54
Checking for null and an array using the && .constructor one-liner
const isArray = (maybeArray) => maybeArray && maybeArray.constructor === Array
const myObject = {}
console.log(myObject && myObject.constructor) // function Object()
console.log(myObject && myObject.constructor.name) // Object
console.log(isArray(myObject)) // false
const myArray = []
console.log(myArray && myArray.constructor) // function Array()
console.log(myArray && myArray.constructor.name) // Array