Playing With Array on Javascript
I’m not a FrontEnd Web Developer. So it’s kind of a note for me.
I have this simple list, or array.
var arr = [23, 12, 1992];
I always use list or array to store data, and sometimes i need to check if some value is already exists on the list. Sometimes i need to check if an index has already exists on the list.
How to check if some value has already exists?
if (list.indexOf(value) !== -1) {
// it did exist
}
How to check if some index has already exists?
if (typeof list[index] !== 'undefined') {
// it did exist
}
That’s it. Well, it’s not the only way of course. You may have another way to check or playing with an array in JavaScript.
One thing, i used ===
so it compared not only the value but the type of the data too.
Some people might say that using ===
are not wise, but hey you can use ==
on above example. ;)
Read other posts