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'
2 Likes

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:

3 Likes

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.

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

2 Likes

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

3 Likes

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:

https://www.codecademy.com/courses/introduction-to-javascript/lessons/advanced-objects/exercises/getters?action=resume_content_item

Thanks!

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

Hi;
I answered the “if” part of question by using “$” :

const robot = {
_model: ‘1E78V2’,
_energyLevel: 100,
get energyLevel (){
if(typeof this._energyLevel===‘number’){
return My current energy level is ${this._energyLevel}.
}
}
};
But it popups error. I got the answer of system, that was:
const robot = {
_model: ‘1E78V2’,
_energyLevel: 100,
get energyLevel(){
if(typeof this._energyLevel === ‘number’) {
return 'My current energy level is ’ + this._energyLevel
} else {
return “System malfunction: cannot retrieve energy level”
}
}
};

console.log(robot.energyLevel);

Why in correct answer, it not used of “$”…
grateful
martinmiciciday

Hi;
Why in “console.log” we not use of :
console.log(robot.energyLevel());
or:
console.log(robot._energyLevel);
because nowhere of our script we used of “energyLevel” alone.
How do I should realize that I have to use console.log(robot.energyLevel);
I mean “energyLevel” alone.
grateful
martinmiciciday

Hi @object7208093151 :slight_smile:

For what I can see about your code, I think what’s missing are a pair of backticks when you use string interpolation , like so:

... {
return `My current energy level is ${this._energyLevel`}
...

I also see a dot at the end of that line… I don’t know if that’s a misptrint or what.

Hope it helps!

I’m rather curious about why using:
if (robot._energyLevel.typeof === 'number')
and using:
if (typeof robot._energyLevel === 'number')
are both acceptable to the editor.
Unless I’m missing something here, shouldn’t appending the TypeOf operator to the end of the property using dot notation throw an error? or does it not matter?
Thanks.