Variable types in JS?

In JS, there are six data types of variable types specified by ES5, namely the basic type string, number, Boolean, undefined, NULL and reference type object. The reference types are divided into Array, Object, Function, Date and RegExp. The basic types are usually identified by typeof. Judgment reference types are usually use constructor, instanceof, Object. The prototype. ToString. Call ()

Determine how to write a variable type

typeofUsage:var a = 1;
    console.log(typeof a)                                            //number
    var b = "hello";
    console.log(typeof b)                                            //string
    var c = [];
    console.log(typeof c)                                            //object
    var d = {};
    console.log(typeof d)                                            //object
    
instanceofusagevar A = [];
    console.log(A instanceof Array)                                  //true
    var B = {};
    console.log(B instanceof Object)                                 //true
    var C = /a/;
    console.log(C instanceof RegExp)                                 //true
    
constructorusagevar aa = [];
    console.log(aa.constructor == Array);                            //true
    var bb = {};
    console.log(bb.constructor == Object);                           //true	
    var cc = 1;
    console.log(cc.constructor == Number);                           //true	
    var dd = "hello";  
    console.log(dd);
    console.log(dd.constructor == String); //true
    
Object. Prototype. ToString. Call the usage ()var a = [];
    Object.prototype.toString.call(a) === '[object Array]';          //true
    var b = {};
    Object.prototype.toString.call(b) === '[object Object]';         //true
    var c = 'hello';
    Object.prototype.toString.call(c) === '[object String]';         //true
    var d = 1;
    Object.prototype.toString.call(d) === '[object Number]';         //true
Copy the code

Js several special variable judgment

The difference between null and undefined?

Null represents a null pointer, which can also be called an empty object. Undefined means that a variable has no value assigned after initialization, and is often used to indicate that a variable is initialized. Typeof NULL returns object.

nullandundefinedUnequal equalitynull= =undefined  // trueFunction types can be used directlytypeofjudgevar a = function(){};
typeof a === 'function'; 
Copy the code