DEV Community

Cover image for JS Test #10: null + undefined
Coderslang: Become a Software Engineer
Coderslang: Become a Software Engineer

Posted on • Originally published at learn.coderslang.com on

7 2

JS Test #10: null + undefined

js-test-10

What’s the difference between null and undefined in JavaScript? What will be logged to the console?
.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

In the first line, we evaluate null === null and the result is true.

In the second line, we evaluate undefined === undefined and the result is true once again.

In the third line, however, we need to understand what the result of null + undefined is. For JavaScript, it’s hard to make sense of what it should be, so it evaluates this expression as NaN.

Now, is NaN equal to NaN?

And the answer is - NO.

In JS NaN is the only value that’s not equal to itself.


ANSWER: The output is going to be true, true, and false.

Learn Full Stack JavaScript

Top comments (1)

Collapse
 
geminii profile image
Jimmy

Mahbe we can precise a little bit but if you need to check NaN you could use isNaN() function to do it 👍

This post blew up on DEV in 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay