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.
>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"