How do I check if a particular key exists in a JavaScript object or array?
If a key doesn\'t exist, and I try to access it, will it return false? Or throw an error?<
In 'array' world we can look on indexes as some kind of keys. What is surprising the in
operator (which is good choice for object) also works with arrays. The returned value for non-existed key is undefined
let arr = ["a","b","c"]; // we have indexes: 0,1,2
delete arr[1]; // set 'empty' at index 1
arr.pop(); // remove last item
console.log(0 in arr, arr[0]);
console.log(1 in arr, arr[1]);
console.log(2 in arr, arr[2]);