Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Behavior in Logial OR

I am experiencing this:

true || "Word"
true

false || "Word"
'Word'

undefined || "Word"
'Word'

I assume it is correct, but I don’t understand the logic behind it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It is pretty simple. For OR operation it returns the first truthy value or false if all conditions are false.

Example:

true || "Word"

Here from left to right if you read the first condtion is true. hence it won’t execute the rest of the part and return true.

false || "Word"

here the first condition is false, hence it will continue checking the next condition which is a string. Any string is considered a truthy value hence it returns the string "Word"

undefined || "Word"

In javascript undefined is considered a falsy. Hence, It returns the next part which is true as it is a string.

now lets check a more complex condition:

false || undefined || "Word" || true

In this exmaple, first condition is false, second is falsy since undefined is considered falsy, third one is true as it is a string. Hence it will return that without going to check the next condition. So final result is "Word"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading