Understanding the use of typeof?

Why does typeof this._energyLevel === ‘number’ evaluate to true when the data type of _energyLevel is 100 not a string ‘number’

1 Like

The comparison is not this._energyLevel === ‘number’, but, typeof this._energyLevel === ‘number’. typeof is evaluated before the comparison.

typeof 100 => 'number'
1 Like

typeof this._energyLevel === ‘number’
In this statement as @mtf stated, typeof is evaluated first, and returns the data type as a string. In this case typeof returns ‘number’ and since ‘number’ === ‘number’ the statement is true.
Check this out:

1 Like

Hello, I don’t understand why testing to see if something is a number requires a
typeof ‘number’
why isn’t it type of num ?

num is a built in thing the computer knows means it has to be a number. I totally don’t get typeof as being ‘number’

There is no data type, num in JavaScript. It is number.

The typeof operator examines a value and determines its data type.


1 Like

okay thank you very much. I am at peace with this.

1 Like

Can someone please explain to me what I may be missing/not understanding in this code?

const robot = {
_model: ‘1E78V2’,
_energyLevel: 100,
get energyLevel(){
if (typeof this._energyLevel){
return 'My current energy level is ’ + this._energyLevel
} else {
return ‘System malfunction: cannot retrieve energy level’

When I try to run it, I get this response:

Did you add an else statement to return the string 'System malfunction: cannot retrieve energy level' ?: expected ‘My current energy level is string’ to equal ‘System malfunction

Thank you!

Is it necessary to know the type of the value when we are getting it? That should not be a concern. We only wish to retrieve the value and report it.

Now if the condition were to be something that checks the amount of energy, then it would make practical sense.

x = this._energyLevel
if (x > 0) {
    return `Current energy level is, ${x}.`
} else {
    return `System malfunction. Energy level ${x} is too low.`

To help with context, please provide a link to this exercise so we may ascertain the expectations.

Gladly! Here it is:



1 Like

Ah, okay, I see now what’s going on. The instructions ask us to check the type, which would make sense only if some other method or direct call is changing the value. This would be rectified in a setter, but that must for somewhere down the line.

x = this._energyLevel
if (typeof x === 'number') {
    return `My current energy level is ${x}`;
} else {
    return `System malfunction: cannot retrieve energy level`;

Thank you! Just to clarify, is there a substantial difference between setting the first argument of the conditional like this:

if (typeof x) {…}

as opposed to this:

if (typeof x === ‘number’) {…}

Or did that only matter for this particular exercise?

We need to carry out the full comparison.

If you only have ‘typeof x’ in the if statement then it will always pass as true and your else will never run. If the data type is a string it will be true and it will be true if the data type is a number or anything else. It has nothing to compare itself to to be useful.

1 Like