Logo

Today I Learned

An empty string is falsy in JavaScript

Instead of using aString.length > 0 to verify if a string has a valid value, you can use the string as the conditional itself.

This is due to an empty string '' being considered falsy in JavaScript:

javascript
1function isNotEmpty(aString) {
2  return Boolean(aString)
3}
4
5isNotEmpty('Hello, World!') // outputs: true
6isNotEmpty('') // outputs: false

The other falsy JavaScript values are:

  1. false
  2. null
  3. undefined
  4. 0
  5. NaN