Shorthand summary of if conditions in js

1. Boolean judgment

var b=true

if(b) is if(true) is equivalent to a shorthand for if(b===true)

2. Numerical judgment

if(n) is equivalent to a shorthand for if(n!==0 && !isNaN(n))

3. Determine whether the string is empty

if(str) is equivalent to a shorthand for if(str!=='')

4. Determine whether the object exists

if(obj) is equivalent to a shorthand for if(obj!==undefined && obj!==null)

//如果对像不存在
if(!obj){
    console.log('对象不存在!')
    //相当于if(obj===undefined || obj===null)的简写
}

4.1. Determine whether the object is empty. if(Object.keys(obj).length>0) has no shorthand. This is the ES6 way of writing.

var obj={}
if(Object.keys(obj).length!==0){
    //对象不为空
    console.log(true)
}else{
    //对象为空
    console.log(false)
}

If ES5, you can use the for in method or convert the json string to determine whether the obj object is empty.

//定义全局变量,定义一个对象person
var person = {name:'张三'}

//定义一个方法,判断对象是否为空。
function isOwnProp(obj) {
    //遍历对象属性。
    //如果有属性就进入循环,找到一个属性就返回true并退出循环和结束函数。
    for (let key in obj) {
        return true
    }
    return false
}

//调用方法进行判断
if (isOwnProp(person)) {
    console.log('对象不为空,拥有自有属性!')
} else {
    console.log('对象为空!')
}

or object to string

if(JSON.stringify(obj)==='{}'){
    console.log('对象为空!')
}
5. Length determines whether the array is empty.
var arr = []
if (arr.length > 0) {
    //数组不为空
} else {
    //数组为空
}
A string is an array of special beads. In addition to having an array subscript, such as str[0], it also has a length attribute. Therefore, if(str.length>0) can also be used to determine whether the string is empty.
length cannot be used to determine whether an object exists, because the object does not have its own length property, that is, obj.length is undefined. And when obj=null or undefined, executing the obj.length program will throw an exception and report an error.
6. Optional chaining operator ( ?. )

if(obj?.age) is equivalent to a shorthand for if(obj && obj.age)

That is shorthand for if( obj != null && obj != undefined && 'age' in obj && obj.age )

?. Multiple splicing can be used. Allows reading the value of a property located deep in an object chain without having to explicitly verify that each reference in the chain is valid.

let name = obj?.sub?.item?.name 

The function of this code is that no error will be reported even if obj, sub or item is undefined.

Precondition, obj must be declared first, otherwise an exception will still be thrown.

That is, if there is a sub attribute in the obj object, there is an item attribute under the sub attribute, and the item object has a name attribute, then return the name value, otherwise return undefined. Regardless of the result, the code executes normally and no exceptions are executed.

If you do not use ?., that is, let name=obj.sub.item.name, as long as one of obj, sub, and item is undefined, the program will throw a reference exception, that is, xxx is not defined and report an error.

Guess you like

Origin blog.csdn.net/qq_21379779/article/details/128574646
Recommended