empty arrays are not falsy

It turns out that in Javascript empty arrays are not falsy:

const xs = [] as number[]
if (!xs) {
  console.log("I'm empty")
}
// doesn't print 'I'm empty'

To test that an array is empty in Typescript, you need to check the length:

const xs = [] as number[]
if (!xs.length) {
  console.log("I'm empty")
}
// does print 'I'm empty'

Would you like to connect? Subscribe via email or RSS , or follow me on Twitter!