Bharathvaj Ganesan
1 min readMar 25, 2018

--

For Checking array’s length it is better to check if it is a defined one (i.e has a value) or may be an array. Since JS is dynamically typed language and doesn’t follow block scoping (ES5) so in the above example the person can be undefined then .length property will throw an error causing the stopping further execution of the script. One can use the following options.

Option 1: if(Array.isArray(person) && person.length) { // block }

Option 2: if(person && person.length) { // block }

--

--

Bharathvaj Ganesan
Bharathvaj Ganesan

Written by Bharathvaj Ganesan

Engineering @ Chargebee | Cybernaut | InfoSec Enthusiast | https://bharathvaj.me

No responses yet