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

5 Likes

Why the typeof operator is not in camil case?

2 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

9 Likes

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

1 Like

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.

14 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.

4 Likes