Why does typeof function(){} returns "function"?

Let’s try the following in your console:

typeof function(){}
// "function"

const test = typeof function(){};

typeof test
// "string"

what?!

1 Like

The typeof operator returns a string saying what the type of something is.

A function is a function, that’s why it returns "function".

A string is a string, which is why typeof "function" returns "string".

4 Likes

Hi!

Because it’s a function :stuck_out_tongue:.

The only confusing part should be why test is a string, in which case it’s because you’re assigning the result of typeof to test, which returns a string, hence test is of type string.

If you expected typeof test to return "function", then you should assign the variable test a reference to one instead of the result of typeof.

Does it help?

2 Likes