Let’s learn how you can check if a key exists in two different ways
Both are very easy.
Firstly let’s define our object
const car = {
brand: "Fiat",
model: "500",
hp: 21,
year: 1960,
}
1. in Operator
just type the key in a string and use “in” with an object
const hasKeyBrand = "brand" in car
// true
2. hasOwnProperty
Here we will use the dedicated function hasOwnProperty()
const hasKeyBrand = car.hasOwnProperty("brand");
// true
And there you go, that’s how you check if the key exists in a javascript array.