Checking if an array is empty with Coffeescript

Alexis picture Alexis · Jun 17, 2014 · Viewed 7.9k times · Source

I am trying to figure out if an array is empty. I tried this and it does not work.

a = []
if a == []
 alert "empty" 

I know I can check the length of the array but am curious why this does not work.

Answer

berrberr picture berrberr · Jun 17, 2014

There is no direct empty check for arrays. You could do something like this though:

a = []
alert("empty") unless a.length