wiki/content/20201113090337-javascript_in_operator.md

746 B

date id title
20201113 4cd0d42b-3414-4402-95f1-7498fbc52c20 JavaScript In Operator

Description

in1 tells us if indices inside an array or object have no associated element.

Syntax

const arr = ['a',,'b']
console.log(0 in arr) // true
console.log(1 in arr) // false
console.log(2 in arr) // true
console.log(arr[1]) // undefined
const car = { make: "Honda", model: "Accord", year: 1998 };

console.log("make" in car); // true

delete car.make;
if ("make" in car === false) {
  car.make = "Suzuki";
}

console.log(car.make); //Suzuki

Footnotes