How do I check in JavaScript if a value exists at a certain array index?
How do I check in JavaScript if a value exists at a certain array index?
Question
Will this work for testing whether a value at position index
exists or not, or is there a better way:
if(arrayName[index]==""){
// do stuff
}
Accepted Answer
Conceptually, arrays in JavaScript contain array.length
elements, starting with array[0]
up until array[array.length - 1]
. An array element with index i
is defined to be part of the array if i
is between 0
and array.length - 1
inclusive. If i is not in this range it's not in the array.
So by concept, arrays are linear, starting with zero and going to a maximum, without any mechanism for having "gaps" inside that range where no entries exist. To find out if a value exists at a given position index (where index is 0 or a positive integer), you literally just use
if (i >= 0 && i < array.length) {
// it is in array
}
Now, under the hood, JavaScript engines almost certainly won't allocate array space linearly and contiguously like this, as it wouldn't make much sense in a dynamic language and it would be inefficient for certain code. They're probably hash tables or some hybrid mixture of strategies, and undefined ranges of the array probably aren't allocated their own memory. Nonetheless, JavaScript the language wants to present arrays of array.length
n as having n members and they are named 0 to n - 1, and anything in this range is part of the array.
What you probably want, however, is to know if a value in an array is actually something defined - that is, it's not undefined
. Maybe you even want to know if it's defined and not null
. It's possible to add members to an array without ever setting their value: for example, if you add array values by increasing the array.length
property, any new values will be undefined
.
To determine if a given value is something meaningful, or has been defined. That is, not undefined
, or null
:
if (typeof array[index] !== 'undefined') {
or
if (typeof array[index] !== 'undefined' && array[index] !== null) {
Interestingly, because of JavaScript's comparison rules, my last example can be optimised down to this:
if (array[index] != null) {
// The == and != operators consider null equal to only null or undefined
}
Popular Answer
Can't we just do this:
if(arrayName.length > 0){
//or **if(arrayName.length)**
//this array is not empty
}else{
//this array is empty
}
Read more… Read less…
Using only .length
is not safe and will cause an error in some browsers. Here is a better solution:
if(array && array.length){
// not empty
} else {
// empty
}
or, we can use:
Object.keys(__array__).length
if(arrayName.length > index && arrayName[index] !== null) {
//arrayName[index] has a value
}
Short and universal approach
If you want to check any array if it has falsy values (like false, undefined, null or empty strings) you can just use every() method like this:
array.every(function(element) {return !!element;}); // returns true or false
For example:
['23', null, 2, {key: 'value'}].every(function(element) {return !!element;}); // returns false
['23', '', 2, {key: 'value'}].every(function(element) {return !!element;}); // returns false
['23', true, 2, {key: 'value'}].every(function(element) {return !!element;}); // returns true
If you need to get a first index of falsy value, you can do it like this:
let falsyIndex;
if(!['23', true, 2, null, {key: 'value'}].every(function(element, index) {falsyIndex = index; return !!element;})) {
console.log(falsyIndex);
} // logs 3
If you just need to check a falsy value of an array for a given index you can just do it like this:
if (!!array[index]) {
// array[index] is a correct value
}
else {
// array[index] is a falsy value
}
if(typeof arr ==='object' && arr instanceof Array ){
if(!arr.length){
println 'empty'
}else{
printn 'not Empty'
}
}else{
println 'Null'
}
If you mean by 'Null' -> Its elements are null or equals to '' , in this case : Check if the array is empty after filtering all 'null' elements
if(!arr.clean().length){return 'is null'}
Of course ,Add Clean method before :
Array.prototype.clean=function(){return this.filter(function(e){return (typeof e !=='undefined')&&(e!= null)&&(e!='')})}