DEV Community

Cover image for JavaScript Interview Question #16: typeof NaN
Coderslang: Become a Software Engineer
Coderslang: Become a Software Engineer

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

6 1

JavaScript Interview Question #16: typeof NaN

js-test-16

What's the type of Not a Number?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

In JavaScript, NaN means Not a Number. This is a special value that appears whenever JS can’t make sense of the numerical expression.

NaN also often appears during typecast. For example, if you try to convert the string into a number, the result will be NaN.

It may seem counterintuitive, but NaN is just a special number. Thus its type is considered to be number.

You can tackle this problem from a different angle and try to answer the question:

What else typeof NaN can be?


ANSWER: the typeof NaN is number, which will be logged to the console.

Learn Full Stack JavaScript

Top comments (0)

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

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

Okay