DEV Community

Cover image for JavaScript Interview Question #40: What is the type of `undefined` in JS?
Coderslang: Become a Software Engineer
Coderslang: Become a Software Engineer

Posted on Originally published at learn.coderslang.com

JavaScript Interview Question #40: What is the type of `undefined` in JS?

js-test-40

What's the output?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

In JavaScript, the typeof operator always returns a string.

So, even though typeof undefined evaluates to undefined, it’s a string and not the primitive undefined value.

The string ’undefined’ is not equal to the value undefined.


ANSWER: the message false will appear in the console.

Learn Full-Stack JavaScript

Top comments (5)

🔴 Hot News: coderslang/javascript interview question what is the type of undefined in js ch - Kapook Update 2025
 
rohithv07 profile image
Rohith V •

So, typeof always returns a string and if we do a check like

typeof undefined === undefined
🔴 Hot News: coderslang/javascript interview question what is the type of undefined in js ch - Kapook Update 2025 🔴 Hot News: coderslang/javascript interview question what is the type of undefined in js ch - Kapook Update 2025

it is actually checking whether the string undefined equal to the type undefined which is a false.
Instead if we do

typeof undefined === "undefined"
🔴 Hot News: coderslang/javascript interview question what is the type of undefined in js ch - Kapook Update 2025 🔴 Hot News: coderslang/javascript interview question what is the type of undefined in js ch - Kapook Update 2025

we will get true as we are comparing with the string "undefined" and with the result which is a string we get from typeof undefined.

🔴 Hot News: coderslang/javascript interview question what is the type of undefined in js ch - Kapook Update 2025
 
coderslang profile image
Coderslang: Become a Software Engineer •

Yes, you're absolutely right

🔴 Hot News: coderslang/javascript interview question what is the type of undefined in js ch - Kapook Update 2025
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ •

If typeof(undefined) == undefined, that'd mean undefined has no type, so it makes sense for the string "undefined" to be returned instead :D

🔴 Hot News: coderslang/javascript interview question what is the type of undefined in js ch - Kapook Update 2025
 
coderslang profile image
Coderslang: Become a Software Engineer •

Hold on, typeof null is coming right after you :)

🔴 Hot News: coderslang/javascript interview question what is the type of undefined in js ch - Kapook Update 2025
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ •

Hey, I never claimed JavaScript is a sane language overall :D