Javascript Check if Not Null and Not Empty String

Aug 21, 2018

The following is not perfect, but work for most common circumstances.

if (value) {}

If you need percise checking

if (value != null && value !== '') {}

If you need to trim white spaces

if (value != null && value.trim()) {  }

You can use lodash isEmpty.

_.isEmpty(null);   // true_.isEmpty('');     // true_.isEmpty('hello') // false_.isEmpty([])      // true_.isEmpty([1])     // false_.isEmpty({})      // true_.isEmpty({test: true})     // false

But the following test return true as well.

_.isEmpty(true);  // true_.isEmpty(false); // true_.isEmpty(1);     // true_.isEmpty(0);     // true

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.