How to smartly use Array.length to your advantage?

Nikhil Nambiar
1 min readFeb 2, 2020

--

Array.length() can come in handy outside of just getting the length of an array. Sometimes, we want to check if there are any elements in the array. Array.length() can come really handy in those times.

Array.length()
let fruits = []

if(fruits) { console.log("print me") }
// print me

if(fruits.length) { console.log("print me") }

fruits = ["banana", "apple"]
(2) ["banana", "apple"]
if(fruits) { console.log("print me") }
// print me

if(fruits.length) { console.log("print me") }
// print me

fruits = []

if(fruits) { console.log("print me") }
// print me

if(fruits.length) { console.log("print me") }
// undefined

--

--