Skip to content

Instantly share code, notes, and snippets.

@kovaldn
Last active September 22, 2016 08:14
Show Gist options
  • Select an option

  • Save kovaldn/5821801 to your computer and use it in GitHub Desktop.

Select an option

Save kovaldn/5821801 to your computer and use it in GitHub Desktop.
Javascript: typeof & $.type()
// typeof
// Знает:
// 1) number
// 2) string
// 3) boolean
// 4) undefined
// 5) object
// 6) function
// Numbers
typeof 37 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // Despite being "Not-A-Number"
// Strings
typeof "" === 'string';
typeof "bla" === 'string';
// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
// Undefined
typeof undefined === 'undefined';
typeof blabla === 'undefined'; // an undefined variable
// Objects
typeof {a:1} === 'object';
typeof null === 'object'; // this is confusing!
// Functions
typeof function(){} === 'function';
// ЛУЧШЕ ИСПОЛЬЗОВАТЬ JQUERY
// знает также:
// 7) date
// 8) regexp
// 9) array
$.type(el)
$.type(true) === "boolean"
$.type(3) === "number"
$.type("test") === "string"
$.type(function(){}) === "function"
$.type([]) === "array"
$.type(new Date()) === "date"
$.type(/test/) === "regexp"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment