akaw301
akaw301

Reputation: 39

what is "is" keyword used for in JavaScript?

so i dont understand the logic behind "is" keyword (here "isSleeping") i see lots of time studying javascript.

does it mean something results boolean?

let animal = {
  walk() {
    if (!this.isSleeping) {
      alert(`I walk`);
    }
  },
  sleep() {
    this.isSleeping = true;
  }
};

let rabbit = {
  name: "White Rabbit",
  __proto__: animal
};

// modifies rabbit.isSleeping
rabbit.sleep();

alert(rabbit.isSleeping); 
alert(animal.isSleeping); 

Upvotes: 2

Views: 1360

Answers (1)

Shashan Sooriyahetti
Shashan Sooriyahetti

Reputation: 878

"is" is not a keyword it's a way of naming your boolean variables AKA naming convention, so when other people see a variable with "is" in front they know that variable is a boolean.

By @ironmouse The same goes with "has": For example there could be functions like "hasSiblings" or "isMarried" that return a boolean type. However the return type is not set in stone but it is a good convention for developers.

Upvotes: 4

Related Questions