&& and || operator in JS explanation . which one is correct? (YDKJS vs FreeCodeCamp)

in YDKJS types and grammar the author stated || and && will give you either the 1st operands or the 2nd operands of the comparison.

in freecodecamp quest it says otherwise.
So which one is correct?

The FCC one is the simple explanation. The YDKJS one is a much more in-depth analysis.

It’s like when they teach you what an atom is in school. They picture a bunch of spheres as protons and neutrons and smaller electrons circling at discreet points along specific paths. Of course with quantum mechanics, the true image of what an atom “looks” like is much more complex, with complex shaped electron probability clouds, etc.

Which explanation is correct? The simple explanation is “correct” to a point and is sufficient for beginners. The more complex explanation is correct for people that want a deeper understanding.

1 Like

i see thanks!!! (20 characters)

Personally, I hate this format/usage

myvar = var1 || var2 
another = var1 && var2 

Why? Because the intent is not clear.

First, you have to be aware that some weird JS shit/stuff is going on before you can understand this code. To me, this is not readable or good code.

What’s so hard about this?

myvar = (var1 != null) ? var1 : var2    // more obvious in it's intent 

// than this
myvar = var1 || var2     // wtf? 


I tend to agree with you, but I come from the world of desktop/mainframe computing, where the length of source code doesn’t matter. In the world of network-delivered scripts, super-concise code leads to faster load times and a better UX at the cost of developer headaches and maintainability. Given that developer experience creates user experience, maybe I’m just playing devil’s advocate here, and there is no excuse :man_shrugging:

i see. when i started to study through YDKJS I had no knowledge of js whatsoever.

so the more “unintuitive” way was the 1st way and only way i’ve learnt.
so I think I am gonna use that…
no offense.

It’s idiomatic in JavaScript, though. It might not be immediately clear to someone coming from another language background, but it should be clear to anyone who’s read and written a decent amount of JavaScript.

To me, this is slightly less readable, if anything. Also, what’s wrong with the compromise below?

myvar = var1 ? var1 : var2
1 Like

Yeah, starting out with YDKJS would be like wanting to learn how to drive a car and start out by learning how fuel injectors work.

Yes, it may be useful information, especially if mastery is your goal, but it is not necessary to basic use and can be intimidating for the beginner.


Off-topic, but your comment reminds me of how Arthur Dent explains tea to the “share and enjoy” beverage meachine.

1 Like