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
}
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
}
if(arrayName.length > index && arrayName[index] !== null) {
//arrayName[index] has a value
}
All real arrays in Javascript contain array.length elements, starting with array[0] up until array[array.length - 1]. There is no way for an array to have "gaps" in it ie specific indexes that just don't exist, unlike languages like PHP - though it is possible for array values to be null, undefined, NaN, Infinity, 0, or a whole host of different values.
Therefore 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 (index < array.length) {
// do stuff
}
I suspect, however, you are not wanting to know if a given value exists, but whether that value is something meaningful. That is, not "undefined", or perhaps, not "undefined or null".
if (typeof array[index] !== 'undefined') {
or
if (typeof array[index] !== 'undefined' && array[index] !== null) {
It depends on what you're trying to achieve.