What is the return value of the || and && operators?

What is the return value of the || and && operators?


|| and && will first perform a conditional judgment on the first operand, if it is not a Boolean value, it will first be converted to a Boolean type, and then the conditional judgment will be performed.

||

For ||, if the condition evaluates to true, it returns the value of the first operand, and if it is false, it returns the value of the second operand.

For example

console.log(1 || null);//1
console.log(null || undefined);//undefined

&&

&& is the opposite. If the condition is true, it returns the value of the second operand, and if it is false, it returns the value of the first operand.

For example

console.log(1 && null);//null
console.log(null && undefined);//null

Summarize

|| and && return the value of one of their operands, not the result of the condition

Guess you like

Origin blog.csdn.net/zhaojiaxing123/article/details/129390355