Why two false the or operator returns the first of the two?

Good evening! There is such code:
<input type="email" placeholder="you@example.com" id="email">


var el = document.getElementById('email'); 
 function isEmpty(element) {
 return !el.value + 'first' || el.value === el.placeholder + 'last';
}
isEmpty(el);


If the field for entering the email does not matter as it should be the first option returns "true first", but if the value in field a, then why it returns again the first option is "false first". Why? After all, was supposed to return the last false value.
5aff0a4010ff7137802618.png
June 5th 19 at 21:02
1 answer
June 5th 19 at 21:04
Solution
Logical or to check the operand is true, the check goes no further and returns its value.
To read
You read the question at all? - frederik.Wyman commented on June 5th 19 at 21:07
read. And you answer, tried to understand at all? Check the operand !el.value + 'first', cast to a string "false first". A string that in turn is converted to Boolean true. The operator returns the first operand, the string "false first". - Lenny.Botsfo commented on June 5th 19 at 21:10
So clear, but if you remove the string 'first' the first option is still the last to false will not be output. Why? - frederik.Wyman commented on June 5th 19 at 21:13
what string "true"? You have to expression this is not. - Lenny.Botsfo commented on June 5th 19 at 21:16
,
var el = document.getElementById('email'); 
 function isEmpty(element) {
 return !el.value || el.value === el.placeholder + 'last';
 }
- frederik.Wyman commented on June 5th 19 at 21:19
, "first' made a mistake. - frederik.Wyman commented on June 5th 19 at 21:22
read about the priorities of logical operators.
You get what is the comparison part !el.value || el.value with part el.placeholder + 'last'. Equivalent to:
(!el.value || el.value) === (el.placeholder + 'last')

To solve this problem is to wrap the comparison in parentheses:
!el.value || (el.value === el.placeholder) + 'last'
- Lenny.Botsfo commented on June 5th 19 at 21:25
Thank you now I understand! - frederik.Wyman commented on June 5th 19 at 21:28

Find more questions by tags JavaScript