Five times x empty
indicates that you have 5 values in your array that are undefined. In the realm of javascript, undefined
and null
hold distinct meanings. Refer to the documentation for further clarification.
To verify null values, simply use if(arr[i]===null){}
Please note that
0,false,null,undefined,""
are all deemed as falsy values. Therefore, if you only utilize:
if(arr[i]){//do something}
else {//do another thing}`
for the aforementioned values, the if
block will never be executed.
Check out this example:
let falsyValues=[0,false,null,undefined,""]
falsyValues.map((val,i)=>{
if(falsyValues[i])
console.log("executing if block with ",val)
else
console.log("else bock executed with ",val)
})
In order to solely search for null
values, you must specifically examine the value.
let arr=[1,undefined,3,undefined,null,6,null,8,null]
arr.map((val,i)=>{
if(arr[i]===null)
console.log("null value found at index ",i)
else if(arr[i]===undefined)
console.log("undefined value found at index ",i)
})