Home > Web Front-end > JS Tutorial > body text

The difference between typeof, instanceof and === in js

WJ
Release: 2020-05-30 13:07:18
forward
2231 people have browsed it

The difference between typeof, instanceof and === in js

The difference between typeof, instanceof and === in js

typeof: used to determine number/string /boolean/underfined type/function, cannot judge: null and object, cannot distinguish object and Array

instanceof: determine the specific object type

===: used to determine undefined and null

//五种基本类型
    var num=1;    var str="abc";    var bl=true;    var nu=null;    var undef=undefined;    //三种特殊类型
    var obj=new Object();    var arr2=["1",2,true];    var fun=function () {
        
    }
    write("-------typeof-----------")
    write(num,typeof num);//1 number
    write(str,typeof str);//abc string
    write(bl,typeof bl);//true boolean
    write(nu,typeof nu);//null object
    write(undef,typeof undef)//undefined undefined
    write(obj,typeof obj);//[object Object] object
    write(arr2,typeof arr2);//1,2,true object
    write("-----------===-----------")
    write(num,typeof num==="number");//1 true
    write(str,typeof str==="string");//abc true
    write(bl,typeof bl==="boolean");//true true
    write(nu,typeof nu==="object");//null true
    write(undef,typeof undef==="undefined")//undefined true
    write(obj,typeof obj==="object");//[object Object] true
    write(arr2,typeof arr2==="object");//1,2,true true
    write(fun,typeof fun==="function");//function () { } true
    write("---------instanceof---------------")
    write(obj,obj instanceof Object)//[object Object] true
    write(arr2,arr2 instanceof Array);//1,2,true true
    write(arr2,arr2 instanceof Object);//1,2,true true
    write(fun, fun instanceof Function)//function () { } true
    write(fun, fun instanceof Object)//function () { } true
Copy after login

The above is the entire content of typeof, instanceof and === in js.

Related references:php中文网

The above is the detailed content of The difference between typeof, instanceof and === in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:51dev.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Latest issues
Popular Tutorials
More>
Latest downloads
More>
web effects
Website source code
Website materials
Front end template
About us Disclaimer Sitemap
PHP Chinese website:Public welfare online PHP training,Help PHP learners grow quickly!