Why is the `typeof` operator formatted differently from other operators?

The first thing I noticed about the typeof operator, was that the syntax is different to other operators used in javascript. In this example, we use the following code:

let newVariable = 'Playing around with typeof.';
console.log(typeof newVariable);     // Prints: string

In my head, I would have thought the following syntax would work, but it doesn’t:

console.log(newVariable.typeof);

This is different from other operators in javascript, such as the .length operator, which returns the length of a string, and can be appended to the name of the variable you are requesting information about. Can anyone comment on why this is so?

.length is a property belonging to a specific data type, like a string.

you should always be able to use typeof, so you don’t want to implement a typeof property for every data type. You just one generic keyword/function to get the type

84 Likes

Why the typeof operator is not in camil case?

15 Likes

Because in JS it is a unary operator and not a method or variable. All the unary operators are written in lowercase.

Expressions and operators

88 Likes

Why is the typeof operator in front of the value whereas a different method (.length) is appended after?

4 Likes

typeof is a global operator so can be called on any object, in any scope or context. .length is an object attribute and can only be called in the context of an existing object instance.

80 Likes

Thank you mtf/Roy. I have learned some good knowledge from you and your posts. I will be going to school at the MDN web-site as soon as I complete all the free courses here. I would sign up and pay for the Pro curriculum here (some of which used to be free btw, i.e(the command line course), but, I cannot afford it.

12 Likes

@mtf comes in with the clutch answers once again

4 Likes

I am having an issue comprehending why typeof doesn’t have a parenthesis. Although I did

console.log(typeof(newVariable));

… and it worked.

So my question is:

> Is it the same? Without the parenthesis? And why the difference in the exercise?

1 Like

It is the same if we consider that the parens are ignored. typeof is an operator, not a function.

3 Likes

…but which of the two, is considered best practice?

1 Like

Best practice in this case would be to not use extraneous tokens. It’s an operator, treat it like one.

typeof value

As far as best practice goes, early on in the course that is the least of one’s concerns. It will eventually surface down the road. Stick to learning concepts, syntax and usage and put the whole notion of best practice and efficiency on the back burner. Burdening one’s self with black and white rules only clouds the water and stymies creative thinking.

13 Likes

Why do we need typeof? Can’t we just tell what type of data it is by looking at it? Like if it has quotes it’s a string, if it says true or false it’s boolean, etc.? Why do we need to know what type of data something is, so badly that typeof is one of the first things we learn in this course?

(sorry if that reads confrontational, it’s not meant to be!)

3 Likes

Maybe, but this becomes very difficult once your project starts to grow in size. Or you might want different behavior based on different types, where a complex function returns different types, and based on the type, you might want to take a specific action.

typeof is really useful, especially because JavaScript doesn’t enforce any types.

sometimes, a concept is explained (typeof in this specific scenario), which might not seem useful at first, but later you will see the value. You might run into this issue more often. The things taught on codecademy are generally quite valuable although they might appear to be on first glance

But explaining all the scenarios how and why certain things are useful might sometimes be overwhelming

4 Likes

Do you think you could explain what “global” is supposed to mean? I see it mentioned often but often only briefly. I understand this might have been asked before so I apologize in advance.

global means accessible to the entire program. Functions and variables at the lowest level are said to be in global scope. They are not contained by any other enclosures (such as a function body) and can be called or polled from anywhere in the program.

5 Likes

I am surprised the answer is number and not integer or float if you assigned 1.1.

There is no float or int type in JavaScript, only ‘number’. We can parse out an integer from a float, or convert an integer to a float. If we need to determine whether the number is one or the other we can draw on the Number method…

Number.isInteger(n)

If it is in fact a ‘number’ and it is not an integer, then it must be a float.

We can also use % to check for a float:

if (n % 1 > 0) {
    // n is a float
}

Mind this won’t tell us that 1.0 is a float so we would have to rely on the earlier method.

7 Likes

I love your answer mtf. :raised_hands:

1 Like

You are so Good with your answers! I love it. Thank you !

1 Like