Home >Web Front-end >Front-end Q&A >What are the data types returned by typeof?

What are the data types returned by typeof?

烟雨青岚
烟雨青岚Original
2020-07-03 14:38:397741browse

The data types that "typeof" can return are: "number", "string", "boolean", "undefined", "object", and "function". Typeof is an operator, the syntax is "typeof (expression)", it performs operations on expressions.

What are the data types returned by typeof?

The data types that "typeof" can return are: "number", "string", "boolean", "undefined", "object" , "function".

typeof is an operator. There are two ways to use it: typeof (expression) and typeof variable name. The first is to operate on expressions, and the second is to operate on variables. Do calculations.

1. If it is a basic data type, return the corresponding basic type

1.number type

var num = 1;
        console.log(typeof num);//返回的是number

2.string type

        var str = 'jack';
        console.log(typeof str);//返回的是string

3. boolean type

    var boo =true;
        console.log(typeof boo);//返回的是boolean

4.undefined type

    var und ;
        console.log(typeof und);//返回的是undefined

2. If it is a complex data type

1. Array type

        var arr = new Array();
        console.log(typeof arr); //返回的是object
        // 2.function类型
        var fn = function(){};
        console.log(typeof fn); //返回的是function

3.Object type

        var  obj = new Object();
        var nul =null;(特别地)
        console.log(typeof nul); //返回的是object
        console.log(typeof obj); //返回的是object

4.Literal array

        var arr2 = [1,32];
        console.log(typeof arr2);//返回的是object

5.Customized object

    function Person(name){
            this.name =name;
        }
        var stu = new Person();
        console.log(typeof stu); //返回的是object
        // 可以得出:复杂数据类型,如果是对象,则返回的是object,如果的function类型,则返回的是function
        // 所以:typeof 可以返回的类型为:number、string、boolean、undefined、object、function

For more related knowledge, please visitPHP中文网! !

The above is the detailed content of What are the data types returned by typeof?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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 admin@php.cn