typeof and more


If we don't know what whether we're working with a string, number, boolean or object we can use the typeof command. Nifty.

var anObj = { job: "I'm an object!" };
var aNum = 42;
var aStr = "I'm a string!";
var aBool = true;

console.log( typeof anObj ); // should print "object"
console.log( typeof aNumb ); // should print "number"
console.log( typeof aStr ); // should print "string"
console.log( typeof aBool ); // should print "boolean"


We can use the hasOwnProperty command to see if an object has a specific property. For example, if were to look at our rocket ship object we could check to see if it had a propulsion or engine property.

var rocketShip = {
    name: "LUNA",
    occupants: 4,
    hull: "armorcrete",
    propulsion: "nuclear rockets",
    acceleration: 8,
    weapons: "nuclear warheads"


console.log( rocketShip.hasOwnProperty('propulsion') ); // should print true
console.log( rocketShip.hasOwnProperty('engine') ); // should print false