Dumb noob question: Why is undefined in quotes?

I’m just watching the 3-hr JS video that FCC has up on YouTube. Why is undefined in quotation marks? Doesn’t that mean it’s looking for a string containing the word “undefined”? From what I learned, quotes around things turns them into strings. I can’t see how undefined would be a string, yet it is typed out like it is a string. Sorry, I am having a hard time trying to explain what I’m thinking here…

To MDN!

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

The typeof operator returns a string indicating the type of the unevaluated operand.

1 Like

typeof always returns a string. "undefined" is not equal to undefined.

2 Likes

HI there. What do you mean

"undefined" is not equal to undefined .

?

"undefined" is a string undefined is a primitive value that indicates that no value has been assigned to the variable or that a function did not return a result.

Hmm. Ok. Um. I still don’t get it. Why, in the example above, is he using a string with the word “undefined” in it? Wouldn’t it be undefined WITHOUT the quotes? I’m sorry. I really suck at JS and this is my 4th time starting over from scratch. Haha. This time I am starting with the Learn JS video. At 56:59 it starts to get too complicated.

typeof returns a string. "undefined" is a string. undefined is not a string.

Ok. So it is just a set rule that typeof will always return a word… and that word can just so happen to be the word “undefined”, right? I can’t say I totally understand WHY it returns a string, and not just a thing that is not a string, but I will keep watching the video and it might all make sense later. Thanks for helping.

It is in the specification of JavaScript that typeof always returns a string. That’s why I linked the MDN reference. When in doubt about what a built in function returns, MDN will always be able to tell you.

1 Like

Because that is the point of the function - it returns a string containing the NAME of the datatype.
If it is an integer it return "integer" (as string), for string it’s "string" (again a string).
And if type is undefined, that typeof returns "undefined".
Reason is, that’s all it’s supposed to do - return the name, so you might compare it to a string or include it in a sentence.

It could return a reference to the datatype itself, but there is no use-case justifying such a behavior. So it’s not designed to do that.
Especially as JS has no strong typing, there is no benefits of having a direct reference to the datatype.

3 Likes

You mentioned that it’s your 4th time starting from scratch, have you tried going through the JS course on freecode camp? it covers those topics with good explanations + solutions and might be easier than following the video (no shade on the video)

typeof tells you what the type of a thing is. A literal undefined isn’t the type of thing, it’s the thing itself.

Like if you do typeof 1, 1 is a type of number, not 1. typeof true, true is a type of Boolean, not true

If I ask you what the thing with two wheels you are cycling along the road on is, you would say “bicycle”, you wouldn’t just hand me the bike; doing the latter wouldn’t be an answer to my question.

Edit: I understand why this particular example is slightly confusing. undefined and the name of its type, "undefined" look visually identical. But undefined is just…undefined, it only has a single value, so the name of the type is the same as the name of the actual keyword itself. Compare "boolean", which covers two possible values (true or false), neither of which look like the name of the type. Or "number", which covers an extremely large number of possible values, none of which look like the name of their type.

5 Likes

There are no dumb noob questions, it’s a contradiction in terms. They would have to come from dumb noobs and the only dumb noobs are the ones that don’t ask questions when they need to.

Look at it this way - if I hire someone to identify fruit, and I hand him a banana, I don’t want him to hand me the banana back, I want him to write down on a piece of paper the word “banana”. typeof does that by returning a string explaining what the type is, it doesn’t return th actual type (which it couldn’t really do anyway).

If these answers don’t clear it up, please ask again so we can figure out how to get batter at explaining stuff. We all, everyone of us, had a couple things that seemed to take forever to click.

5 Likes

Thanks to everyone for the replies. I have re-started the JS course… trying to go slower. I am getting hung up on the “Stand In Line” lesson, but I will do my best to figure it out on my own.

As for the undefined thing… yeah it’s pretty confusing, but I will keep trying to wrap my brain around it. Thanks again.

Feel free to ask for help when you get stuck. Sometimes taking the time to describe how we’re stuck and talking to others about the problem helps get your brain flowing in the right direction!

I’m not sure what you mean. undefined has a single meaning. "undefined" has a single meaning. They have different meanings.

1 Like

I’m sorry, I have no idea what you’re trying to say. Your comment doesn’t seem related to the conversation in this topic.

Many of us provided correct information for this question above. You seem to be confusing a previously solved topic by providing incorrect information. I try to fix incorrect information on the forum, as it can be confusing for learners.

1 Like

Ok Thx, I`m just relaying information from an API project that had missing quotation marks in the log as undefined so I will recheck this.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.