+ 3

What is the output of this code???

alert("john" || "user"); // Answer is john.. alert("john" && "user"); //Answer is user Does anyone know how the answer came about?

18th Feb 2021, 4:23 AM
S.P. Karthee
S.P. Karthee - avatar
13 Answers
+ 11
|| and && in JS don't return true or false like other lenguages, but the first value that halts the short-circuit evaluation That means that || returns the first true value or the last false one while && returns the first false value or the last true one
18th Feb 2021, 4:45 AM
Angelo
Angelo - avatar
+ 3
https://en.m.wikipedia.org/wiki/Truth_value https://en.m.wikipedia.org/wiki/Short-circuit_evaluation
18th Feb 2021, 4:59 AM
Angelo
Angelo - avatar
+ 2
Thx! I got it! expr1 || expr2 If expr1 can be converted to true, returns expr1; else, returns expr2. expr1 && expr2 If expr1 can be converted to true, returns expr2; else, returns expr1.
18th Feb 2021, 6:02 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Yes! js is generally a strange language and you need to know its features. https://www.sololearn.com/post/207103/?ref=app
18th Feb 2021, 6:10 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Thanks 🙂
18th Feb 2021, 7:05 AM
S.P. Karthee
S.P. Karthee - avatar
0
Sololearn's one of challenge question
18th Feb 2021, 4:43 AM
S.P. Karthee
S.P. Karthee - avatar
0
Angelo how js determines that John is true in the first alert, and the user is true in the second alert?
18th Feb 2021, 4:50 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Ярослав Вернигора(Yaroslav Vernigora) they are not empty strings True and false state of variables is not really about truth, it's more about 1s and 0s
18th Feb 2021, 4:52 AM
Angelo
Angelo - avatar
0
I still didn't get it. where can I read about it? carefully study the operation of logical operators?
18th Feb 2021, 4:55 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Guys, js logical operates are different from other languages???
18th Feb 2021, 6:07 AM
S.P. Karthee
S.P. Karthee - avatar
18th Feb 2021, 6:59 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar