Values And Types

Types

There's 6 javascript types, and will only return one of 6 (7 in in ES6).

  • String
  • Number
  • Boolean
  • Object
  • Null and Undefined
  • Symbol (ES6)

Important the typeof operator always returns the result as a string. So comparison would be something like

if (typeof(a) === "string") {
 
}
 
if (typeof(a) === "undefined") {
 
}
var a;
typeof a; // "undefined"
 
a = "hello world";
typeof b; // "string"
 
a = 42;
typeof a; // "number"
 
a = null;
typeof null; // "object" -- weird bug
 
a = undefined;
typeof a; // "undefined"
 
a = { a: 1 }
typeof a; // "object"
 
/** Functions **/
 
// See http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.3
 
function amount() {
    return 42;
}
 
amount.tax = 20;
 
typeof amount(); // "number"
typeof amount; // "function" -- it has a call so returns "function" and not "object" (see link)
typeof amount.tax // "number"
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License