Created
November 17, 2020 19:52
-
-
Save DoctorDerek/3be6f651b52395ff62ff52cd8edd7423 to your computer and use it in GitHub Desktop.
Checking for an array using the .constructor property
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 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 | |
| console.log(isArray(myArray)) // true | |
| const myNull = null | |
| try { | |
| console.log(myNull.constructor) | |
| } catch (e) { | |
| console.log(e) | |
| } // TypeError: "myNull is null" | |
| try { | |
| console.log(null.constructor) | |
| } catch (e) { | |
| console.log(e) | |
| } // TypeError: "null has no properties" | |
| try { | |
| isArray(myNull) | |
| } catch (e) { | |
| console.log(e) | |
| } // TypeError: "maybeArray is null" | |
| const myDate = new Date() | |
| console.log(myDate.constructor) // function Date() | |
| console.log(myDate.constructor.name) // Date | |
| console.log(isArray(myDate)) // false | |
| const myRegExp = /howdy/ | |
| console.log(myRegExp.constructor) // function RegExp() | |
| console.log(myRegExp.constructor.name) // RegExp | |
| console.log(isArray(myRegExp)) // false | |
| const myUndefined = undefined | |
| try { | |
| console.log(myUndefined.constructor) | |
| } catch (e) { | |
| console.log(e) | |
| } // TypeError: "myUndefined is undefined" | |
| try { | |
| console.log(undefined.constructor) | |
| } catch (e) { | |
| console.log(e) | |
| } // TypeError: "undefined has no properties" | |
| try { | |
| isArray(myUndefined) | |
| } catch (e) { | |
| console.log(e) | |
| } // TypeError: "maybeArray is undefined" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment