DEV Community

front_end_shifu_2022
front_end_shifu_2022

Posted on

Logical operators(|| OR)

|| (OR)

the logical OR Return true If any of its arguments are true otherwise it returns false..
e.g.
alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false

If an operand is not a boolean, itโ€™s converted to a boolean for the evaluation.

For instance, the number 1 is treated as true, the number 0 as false
e.g.
if (1 || 0) {
alert( 'truthy!' );
}
in above the output is truthy.cause true||false return True.

Now lets talk about multiple ORโ€™ed values:
result = value1 || value2 || value3;

The OR || operator does the following:
Evaluates operands from left to right.
For each operand, converts it to boolean. If the result is true, stops and returns the original value of that operand.
If all operands have been evaluated (i.e. all were false), returns the last operand.
e.g.
alert(''||0||1);//return 1 which is first truthy value
alert(null,0,undefined);// all falsy ,return last value.
*
let person1='';
let person2;
let person3='talha';
alert(person1||person2||person3||'no one');*
above output is talha.

Top comments (0)