The distinction between typeof instance of null undefined

Keywords: Javascript Attribute

Use of 1 typeof; this is an operator, not a method

1.1 Detects the data type and returns a string type. There are six possibilities: number Boolean string object function undefined

Note that typeof(null) returns: object

1.2 typeof can be used for declared or undeclared variables; however, undeclared variables cannot be operated on by other operators, which can cause errors because other operators can only be used on declared variables.

<script>
    var exp1 ;
    console.log(typeof exp1);//undefined
    console.log(typeof exp2);//undefined
    console.log(exp1 == undefined);//true   
    console.log(exp2 == undefined);//Report errors
    console.log(typeof exp1 == 'undefined');//true
    console.log(typeof exp2 == 'undefined');//The true typeof operator can be used for undeclared variables
</script>

1.3 The typeof value for the basic data type (Number Boolean String null undefined) and the reference type object

Class 1.3.1: Object Function Array String Boolean Number Date; these classes that are not initialized as instances have a typeof type of function

    function test (){       }
    console.log(Object);//function Object() { [native code] }
    console.log(typeof Object);//function
    console.log(Array);//function Array() { [native code] }
    console.log(typeof Array);//function
    console.log(Function);//function Function() { [native code] }
    console.log(typeof Function);//function
    console.log(String);//function String() { [native code] }
    console.log(typeof String);//function
    console.log(test);//function test(){   }
    console.log(typeof test);//function

1.3.2 Objects are created by following the name of the instantiated class with the keyword new. When a function is instantiated through new, an object is created.

    function test (){       }  
    var obj = new test();
    console.log(obj.constructor);//function test(){}
    console.log(obj);//test{}
    console.log(typeof obj);//Object

    var obj1 = new Array();
    console.log(obj1.constructor);//function Array() { [native code] }
    console.log(obj1);//[]
    console.log(typeof obj1);//Object

    var obj2 = new Function();
    console.log(obj2.constructor);//function Function() { [native code] }
    console.log(obj2);//function anonymous() {}
    console.log(typeof obj2);//function

    var obj3 = new String();
    console.log(obj3.constructor);//function String() { [native code] }
    console.log(obj3);//String {length: 0, [[PrimitiveValue]]: ""}
    console.log(typeof obj3);//object

    var obj4 = new Object();
    console.log(obj4.constructor);//function Object() { [native code] }
    console.log(obj4);//Object {}
    console.log(typeof obj4);//object

1.3.3 Value of typeof for basic data types

<script>
    var func = function(){
        console.log("Hello");
    }
    var obj = {"name":"john"};          
    console.log(typeof 5);            //number
    console.log(typeof 'name');        //string
    console.log(typeof false);        //boolean
    console.log(typeof null);        // object
    console.log(typeof undefined);    // undefined
    console.log(typeof func);        // object
    console.log(typeof obj);        // object
//All types of return values are string types, note that they are lowercase letters; but one drawback is that the values returned by functions and objects and DOM objects are objects, so when typeof is used to monitor data types, it is more reliable to monitor basic data types, but it is not very useful to monitor objects.
</script>

1.3.5 Through the above analysis, we find that typeof can judge the basic data type, but for complex data type, return is all object, so how to detect the "class" of object? That is, how to detect which constructor an object was created by?

This is the time to use instanceof; grammar: obj instanceof Type, will follow the prototype chain of the object layer by layer, if found by type, return true, otherwise return false;

    function test (){       }
    var obj = new test();
    console.log(obj.constructor);//function test(){}
    console.log(obj);//test{}
    console.log(typeof obj);//Object
    console.log(obj instanceof test);//When true denotes an obj object, the constructor test produces

2 null undefined

2.1 null is an object that represents "nothing" and converts to a numerical value with a value of 0. Typical uses are:

  • Used to initialize a variable that may be assigned to an object in the future

  • Used to compare with an object that has been initialized. This variable can be an object or not.

  • When the parameter expectation of a function is an object, it is used as a parameter input.

  • When the expected return value of a function is an object, it is treated as the output of the return value.

  • Delete event bindings. The event itself is a null, an empty object that can be added

  • As the Endpoint of Object Prototype Chain

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    <input type="button" value="Button"/>
    <script>
        console.log(document.querySelector("input").onclick);//null
        document.querySelector("input").onclick = function(){
            console.log("Be defined");
        };
        console.log(document.querySelector("input").onclick);//Function () {console. log ("defined");}
    </script>
    </body>
    </html>
    

2.2 undefined is a primitive value for "nothing", which is converted to a value of 0. Typical usage is:

  • A variable is declared, but it is not assigned, so the value of the variable is undefined.

  • When a function is called, if the parameter that should be provided is not provided, the default is undefined.

  • If the attribute of an object is not assigned, the attribute value is undefined

  • When the function does not return a value, undefined is returned by default.

2.3 How to distinguish the two?

  • == Operators only compare values, not type comparisons. Before comparisons, they are implicitly converted, null==undefined returns true.

  • === Distinguishing between the two is not only a comparison of content, but also a comparison of data types null=== undefined false

How to determine whether a variable is null or undefined?

  • How to determine a variable is undefined;

    Var exp = undefined; / var exp; if not assigned, the result is undefined
    // This method is incorrect because null==undefined returns true;
    if(exp == undefined){
    console.log("exp variable is undefined");
    }
    // This method is correct. Notice undefined plus ", because the value returned by typeof is of a string type.
    if(typeof(exp) == 'undefined'){
    console.log("exp variable is undefined");
    }

Attached below is the W3C explanation of the == operator to facilitate the reader's recall of the basis.

Rules governing the implementation of type transformation:
If an operand is a Boolean value, convert it to a numeric value before checking for equality. false converts to 0, true to 1. 
If one operand is a string and the other is a number, try to convert the string into a number before checking for equality. 
If one operand is an object and the other is a string, try to convert the object into a string before checking for equality. 
If one operand is an object and the other is a number, try to convert the object into a number before checking for equality.
The following transformation rules are also followed:
The values null and undefined are equal. 
When checking equality, null and undefined cannot be converted to other values. 
If an operand is NaN, the equal sign returns false and the non-equal sign returns true. 
If both operands are objects, their reference values are compared. If two operands point to the same object, the equal sign returns true, otherwise the two operands are unequal.

I think it can be simplified to explain the comparative process.

If one of them is a Boolean type, the Boolean type is first converted to 0 or 1;
If an object is an object, it is converted to the original value when compared with a string, number, or Boolean type. The object looks at the original value through toString valueOf, and then compares it with the string number Boolean type.
  • How to determine a variable is null

    var exp = null;
    // The following two are incorrect
    If (! Exp) {//0 or undefined can also enter the if statement
    console.log("the variable is null");
    }
    If (exp = null) {//undefined = null returns true
    console.log("This variable is null")
    }
    // The following is the correct way to judge null type
    If (typeof (exp) =='object'& & exp== null) {// Judge both type and content at the same time
    console.log("the variable is null");
    }

Posted by anon_amos on Sun, 07 Apr 2019 17:39:30 -0700