Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why you can check if array exists in Javascript with .length

As I Java programmer I do not understand why you can check if the array is empty using if(array.length). Can someone please elaborate?

like image 803
Gaetano Herman Avatar asked Mar 09 '23 04:03

Gaetano Herman


1 Answers

Actually array.length is evaluated as true when it has at least one element. When array is empty array.length returns 0 which is evaluated as false.

So the bottom line is you can use if (array.length) to check if array is NOT empty.

like image 103
begie Avatar answered Apr 26 '23 22:04

begie