DEV Community

Discussion on: Javascript ||, aka Why Doesn't This Work?!

Collapse
 
lexlohr profile image
Alex Lohr

There are two finer points of the language that may be confusing sometimes even to experienced developers.

The first one is type coercion, which is an automatism to align values for the task at hand. For example, a logical operator will coerce any value to a boolean for the sake of evaluation. An empty string is falsy, a string containing characters isn't. There's quite a complex set of rules to this, and it makes the language easily confusing.

That brings us to the second one, which is operator precedence. I'll spare you the details, it's just that some operators are processed prior to other ones.

Especially in combination, these two points lend themselves to a many WTF-moments when writing JavaScript.

Collapse
 
laurieontech profile image
Laurie

No question. And that’s the former is what most of this post focuses on. It resolves each side of the operator to a Boolean and operates accordingly.