当前位置:网站首页>01js basic null and undefined difference type conversion = = code block logical operator

01js basic null and undefined difference type conversion = = code block logical operator

2022-06-10 21:19:00 It's the cloud!

null And undefined difference

From the beginning of the design

undefined: When a variable is declared unassigned , The printout is undefined

null: When you want to declare an object but are not sure , Sure var obj = null

// When declaring a variable object {} And null difference 
var obj1 = {}
var obj2 = null
if(obj1){
// Here the code will be executed 
}

if(obj2){
// Here the code will not be executed 
}

Type conversion

string

Implicit conversion :+ As long as one is a string type ,+ It's splicing

Display conversion :String()

number

Implicit conversion :*/

Display conversion :Number()

boolean

Implicit conversion : If it is empty intuitively, it will turn to false,0/""/null/undefined/nan, Others are converted to true

Display conversion :Boolean()

== And === difference

== When making judgments , When two operation metadata types are different , Will convert the operand to number type , Then judge ,null Or object types

Situation 1 :null==undefined true

Situation two :NaN == NaN false

Situation three : Number type == String type String type ToNumber()

Situation four : Boolean Type will also ToNumber()

Situation five :Number || String == Object  || null    Will convert the complex type to the original type for    ToPrimitive(obj/null)

//  here null Although after ToPrimitive(), But it is returned directly by the function , There is no transformation , therefore false
console.log(123 == null) // false

//obj Object to carry out == When judging , Would call ToPrimitive() To the original object , How to implement the internal functions , We don't delve into , But we can rewrite 
var obj = {
    name : "zhao",
    [Symbol.toprimitive](){
        return 123
    }
}

console.log(123 == obj) // true

=== Strict comparison does not carry out type conversion

Code block

stay js You can use curly braces to represent code blocks in , such as :if Judge for loop ... We can control code block execution through branch statements and loops

{
    var name = "zhao"
    var age = 18
}

Be careful : Object is not a code block ,

Logical operators

Logic or   a || b    Just one for true, Namely true

The essence : Short circuit or   res = a || b || c  abc Are operands From left , To Boolean type , find true Just return the original data , Will not continue to judge downward , If all the operands are false Just return the original data of the last operand

function getMessageLength(message) {
    var mes = message || " The default value is "   //  Most commonly used in development 
    return mes.length
}

Logic and a && b As long as there is one false Namely false

The essence : Short circuit and res = a && b && c From left , To Boolean type , find false Just return the original data of the modified operand , Will not continue down , If it's all true, Just return the last operand metadata

var obj = {
    name : "zhao",
    friend : {
        name: "li",
        eating: function() {
            console.log("eating")
        }
    }
}

obj && obj.friend && obj.friend.eating && obj.friend.eating() //  It's safer 

Logic is not !

To Boolean type    var  message = "hello world"  !!message

原网站

版权声明
本文为[It's the cloud!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102011343994.html