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

Coderslang: Become a Software Engineer @coderslang

About: Teaching you to code at js.coderslang.com - JavaScript, HTML, CSS, Node.js, React.js, React Native

Joined:
Nov 27, 2020

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

Publish Date: May 13 '21
30 5

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

Comments 5 total

  • 𒎏Wii 🏳️‍⚧️
    𒎏Wii 🏳️‍⚧️May 14, 2021

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

  • Rohith V
    Rohith VMay 14, 2021

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

    typeof undefined === undefined
    
    Enter fullscreen mode Exit fullscreen mode

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

    typeof undefined === "undefined"
    
    Enter fullscreen mode Exit fullscreen mode

    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.

Add comment