data type determination js

1. typeof operator

  typeof basic data types based on:

  typeof 123; // "number"

  typeof 'abc'; // "string"

  typeof true; // "boolean"

 

  Encountered where complex data types:

  typeof {}; // "object"

  typeof []; // "object"

  was f = function () {};

  typeof f; //'function'

  

  Special case:

  1.typeof null; // "object" This is a question left over from history, not to get to the bottom, is a hidden pit

  2. typeof undefined; (undefined refers to an undefined variable) // "undefined" 

  Using this feature for determining sentence

if (v) {
  // ...
}
// ReferenceError: v is not defined

// 正确的写法
if (typeof v === "undefined") {
  // ...
}

 

In summary, the use of value typeof get there "number", "string", "boolean", "object", "function", "undefined" These six values

 

2. instanceof operator

  For such an array of objects and data, we can not be distinguished using typeof, this time the need to use instanceof operator

var o = {};
var a = [];

o instanceof Array // false
a instanceof Array // true

 

Guess you like

Origin www.cnblogs.com/ikumi/p/11309772.html