How does it work?

i’m really confused =

const robot = {
  _model: "1E78V2",
  _energyLevel: 2,
  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);

at line 5 we wrote "number" OK , and its just a string , but how JavaScript knows that "number" have to be a number? how does it understands an English word (string) ?

Documentation is always a great resource to explore details about operators, methods, and other features of the programming language.

Documentation for typeof: typeof - JavaScript | MDN

The documentation mentions:

The typeof operator returns a string indicating the type of the operand’s value.

The documentation also includes some examples.

3 Likes

wow , so basically the JS engine will detect that , thanks

i have one request , since you are a gold problem solver , if you like = i have created a topic last day but nobody answered , maybe you can answer it here is the topic =