0

When I use array.length != 0 to reference the condition of a non-empty array for a while loop, the rest of the code works fine. However, if I use array != [] to reference while an array is not empty, the same code gets TypeError (Cannot read properties of undefined) for the while loop.

I'm new to JavaScript. I thought either could mean the array is not empty, but apparently not. Can someone help explain the difference between array.length != 0 and array != []? Thank you!!

wukong
  • 1
  • Visit http://www.jsfuck.com/ and take a look at the `Basics` – Ephellon Grey Nov 11 '22 at 16:31
  • 1
    JavaScript gives *references* to arrays. `[]` is a new array that has its own reference. These references are not equal. Not so with primitives, like numbers, where the comparison works as you intended. – trincot Nov 11 '22 at 16:32
  • @trincot Thanks so much for your quick and helpful response! I understand better now! – wukong Nov 11 '22 at 19:04

0 Answers0