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
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.
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.
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.
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!)
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
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.
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.