+ 1
Logical operator
Hi class, can anyone explain me: If we define to string variables string1 and string2 in Java script, both is not null strings (not undefined strings), and I compare bot using or operator || , what wil be the output? Thank you.
2 Answers
+ 7
If both string1 and string2 are not null and not undefined, and you compare them using the logical OR operator ||, the output will depend on the content of the strings. The logical OR operator || returns true if at least one of its operands evaluates to true.
Likewisely,
0 || 1 = 1
1 || 0 = 1
1 || 1 = 1
Here, 1 represent TRUE, and 0 represent FALSE. In the context of comparing your strings, the operands are treated as truthy values if they are not empty strings ("").
0
To add a little detail on case you run into a code challenge similar to this post.
https://www.sololearn.com/discuss/3274520/?ref=app
JS use short circuit logic evaluation too.
Here is a code to illustrate how it works.
https://sololearn.com/compiler-playground/cRsloLy0eCPX/?ref=app