r/javascript • • May 28 '19

What are some JavaScript things beginners don't know?

333 Upvotes

383 comments sorted by

View all comments

Show parent comments

45

u/[deleted] May 28 '19

Most of these have nothing to do with coercion.

I guess you're right about people struggling with it.

16

u/[deleted] May 28 '19

let me end all your struggles:

the only time you use == instead of === in JS is when you do yourVar == null because this checks for null and undefined.

2

u/[deleted] May 28 '19

Your data structures shouldn't usually be capable of being T | null | undefined, so I'd argue there's really no place for double equals in a modern codebase.

5

u/[deleted] May 28 '19

Kyle Simpson would disagree. Coercion can be useful and is a valuable part of JavaScript to understand.

2

u/Mydrax May 28 '19

Coercion leads to an unpredictability and that means that the uncertainity is high therefore there's a risk for code to be erroneous. A risk that should usually be avoided by the language itself.

In simpler situations it might be okay-ish but in large codebases, with other things that contend to the unpredictable nature of Javascript, it does get messy and difficult and this holds specially true for data structures!

3

u/[deleted] May 28 '19

I agree in some cases, but I still think it’s important to know. Many codebases use checks like

if(!someVar) { ... }

Which coerces someVar into a Boolean. While I rarely use ==, I don’t think core concepts of JavaScript should be avoided or overlooked.

→ More replies (2)