r/javascript • • May 28 '19

What are some JavaScript things beginners don't know?

333 Upvotes

383 comments sorted by

View all comments

28

u/DerGernTod May 28 '19

coercion in general

false == 'false' // false
false == '' // true
false === '' // false
var x = false || 'foo' // x is 'foo'
var y = true || 'foo' // y is true
var z = y && x // z is 'foo'

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.

0

u/[deleted] May 28 '19

Let me be a devils advocate: If you ever have a bug caused by coercion, your code sucked in the first place and it's not coercion that caused it. So use == or ===, it doesn't make a difference.