To determine the type of an unevaluated operand
typeof undefined === "undefined"
typeof null === “object”
typeof true === “boolean”
typeof 3.14 === “number”
typeof “good” === “string”
typeof Infinity === “number”
typeof NaN === “number”
typeof new Date() === “object”
typeof {a:1} === ‘object’
typeof [1, 2, 4] === ‘object’; // use Array.isArray or Object.prototype.toString.call to differentiate regular objects from arrays
To differentiate between the various typeof objects
var toClass = {}.toString
console.log( toClass.call( [1, 2] ) );
To determine if an object, is an instance of a class
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
var mycar = new Car("Honda", "Accord", 1998);
var a = mycar instanceof Car; // returns true
var b = mycar instanceof Object; // returns true
Reference
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
Tags: JavaScript
Leave a comment