JS method for judging data type

catalogue

1, Data types in JS

2, Methods of judging data types in JS

1. Using typeof

2. Using instanceof

3. Use object prototype. toString. call

4. Using constructor

1, Data types in JS

ECMAScript has five simple data types (also known as basic data types): Undefined, Null, Boolean, Number, and String. There is also the complex data type object in 1. Object is essentially composed of a group of unordered name value pairs.

Undefined, Null, Boolean and Number are all basic types.

Object, Array and Function are reference types,

data typeexplain
Value type NumberCan only be numbers or decimals
String type stringString type string, any character wrapped in single quotation marks or double quotation marks
Boolean typeCan only be true or false for true or false
undefinedAfter a variable is defined, it is not assigned a value. This variable is undefined

null is the object type,

A null value indicates a null object pointer
Object type objectThere are many kinds, such as array object, mathematical object and Date object

2, Methods of judging data types in JS

1. Using typeof

let obj={
            name:'dawn',
            age:21
        }
        let fn=function() {
            console.log('I am function type');
        }
        console.log(typeof 1);       //number
        console.log(typeof 'abc');   //string
        console.log(typeof true);    //boolean
        console.log(typeof undefined);  //undefined 
        console.log(typeof fn);      //function
        console.log(typeof (new Date) );  //object
        console.log(typeof null);     //object
        console.log(typeof [1,2,3]);  //object
        console.log(typeof obj);      //object

The results show that typeof can test number, string, boolean, undefined and function,

[note]: for null, array and object, typeof is detected as object, and their types cannot be further judged.

2. Using instanceof

obj instanceof Object can put the content you want to judge on the left and the type on the right for JS type judgment. It can only be used to judge complex data types, because instanceof is used to detect whether the # prototype # attribute of the constructor (right) appears on the prototype chain of an instance object (left).

 let arr=[1,2,3,4,5,6,7]
        let obj={
            name:'dawn',
            age:21
        }
        let fn=function() {
            console.log('I am function type');
        }

       console.log(arr instanceof Array);  //true
       console.log(obj instanceof Object);  //true
       console.log(fn instanceof Function);  //true
       console.log((new Date) instanceof Date);  //true

3. Use object prototype. toString. call

let obj = {
            name: 'dawn',
            age: 21
        }
        let fn = function () {
            console.log('I am function type');
        }
        
        console.log(Object.prototype.toString.call(1));        // [object Number]
        console.log(Object.prototype.toString.call('Hello tomorrow')); // [object String ]
        console.log(Object.prototype.toString.call(true));     // [object Boolean]
        console.log(Object.prototype.toString.call(undefined));  // [object Undefined]
        console.log(Object.prototype.toString.call(fn));   // [object Function]
        console.log(Object.prototype.toString.call(new Date));  // [object Date]
        console.log(Object.prototype.toString.call(null));   // [object Null]
        console.log(Object.prototype.toString.call([1, 2, 3]));  // [object Array]
        console.log(Object.prototype.toString.call(obj));       // [object Object]

Calling Object's native toString() method on any value will return a string in [object NativeConstructorName] format. Each Class has a [[Class]] attribute inside, which specifies the constructor name in the above string.
However, it cannot detect the constructor name of a non-native constructor.

4. Using constructor

let arr = [1, 2, 3, 4, 5, 6, 7]
        let obj = {
            name: 'dawn',
            age: 21
        }
        let fn = function () {
            console.log('I am function type');
        }

        console.log((9).constructor === Number);  //true
        console.log('hello'.constructor === String);  //true
        console.log(true.constructor === Boolean);  //true
        console.log(fn.constructor === Function);  //true
        console.log((new Date).constructor === Date);  //true
        console.log(obj.constructor === Object);  //true
        console.log([1, 2, 3].constructor === Array);  //true

[note]: the constructor cannot judge undefined and null, and it is not safe to use it, because the direction of the constructor can be changed

 

Keywords: Javascript Front-end

Added by sqlnoob on Mon, 31 Jan 2022 04:38:11 +0200