Why does the logical OR operator returns this! | Sololearn: Learn to code for FREE!


Why does the logical OR operator returns this!

Alert(“John” || “user”) //returns John Why does this return the string John? I would expect it to return True? P.S this code is from a code challenge by user @kimwele

11/29/2021 10:42:35 PM


3 Answers

New Answer


Hi, I am VCoder, Here I'll explain you wath's the heck : In JavaScript, boolean operators are a bit different from whose from other coding languages, In JavaScript, the OR operator will return the first truly value (true, not empty string, number other than 0, not empty array or object ) which are his operands, but if both of them are falsy values (empty string, array or object, 0 and false) the operator will return false And this the same principle with the AND operator If you want that it return a boolean value, you can do this : Boolean("John" || "user") ; I am making a serie of tutorials about a lot of things which are related with coding, so you can visit this page and say me what you want to learn : https://code.sololearn.com/WIb425hnGqfl/?ref=app If you "follow" me, I'll be able to give you everyday coding tricks Thank you and Good luck


Thanks VCoder


You're welcome !