+ 2

Why is the answer "John" if the question alert("John" || "user")? And what does || actually mean in JavaScript?

When I am challenging someone, there's a question like alert("John" || "user"). If I check the correct answer, it is answer "John", and I thought the answer is only true and false. What is that??

26th May 2019, 3:33 PM
Rizki Rivaldi
2 Respuestas
+ 2
If the first argument is true, use this, else use second argument. See this https://stackoverflow.com/questions/37318146/javascript-logical-operators-and-strings-numbers
26th May 2019, 3:52 PM
Jônatas Araripe
Jônatas Araripe - avatar
+ 1
|| simply means or. And it picks the first true value. For it to pick "user" the first value has to be false, NaN, Undefined, null etc.
26th May 2019, 6:00 PM
Wakasa MG Lavender
Wakasa MG Lavender - avatar