#truth value vs falsie values
7 messages · Page 1 of 1 (latest)
If you notice in first picture, the variable response assigned an empty string (""), and hasValidEmail is assigned the boolean value true. The variable user is then assigned the result of the logical AND (&&) operation between hasValidEmail and the expression (response || "guest").
In js, the logical AND operator (&&) operator returns the value of the second operand if the first operand evaluates to true. If the first operand evaluate to false, then expression cutoff and first operand returned.
And here, hasValidEmail is true, the expression (res || "guest") is evaluated. The logical OR (||) operator returns the value of the first truthy operand encountered, or the value of the last operand if all operands are falsy.
Since here, response is empty string and in JS empty string is falsy value, so it evaluate to false. the next operand is the string "guest", which is truthy value. Therefore, the expression (response || "guest") evaluate "guest".
So, in the end the expression hasValidEmail && (res || "guest") will return "guest" in this case.
In second picture, response is "JohnDeo" which is truthy value, therefore the expression (response || "guest") evaluates to "JohnDeo". so user will give "JohnDeo".