What’s an effective way to check if a value is not null and not an empty string in JavaScript?

I’m currently doing this:

var data; // fetched from DB if (data != null && data != ‘’) { // do something } Is there a cleaner or better approach to verify that JavaScript is not null and also not an empty string? Appreciate any tips!

I’ve been working with front-end apps for a few years now, and here’s a pattern I rely on when checking if a value in JavaScript is not null or an empty string. Especially when user input is involved, strings with just whitespace can sneak in and cause trouble. So, I usually go with this:

if (data !== null && data !== '' && data.trim() !== '') {
  // do something
}

This way, even if data is just " ", it’ll be caught. It’s super useful when you want clean, reliable input before moving forward.

Yeah, I’ve bumped into this pattern often enough that I just wrapped it in a helper. When you’re building anything with a team, clarity and consistency are gold. Here’s how we handle checking if JavaScript is not null or empty in our codebase:

function isNotNullOrEmpty(value) {
  return value !== null && value !== '' && value.trim() !== '';
}

// Usage
if (isNotNullOrEmpty(data)) {
  // do something
}

Makes things easier to read, and no one has to second guess what the condition is doing. Plus, having it in one place helps if we need to tweak the logic later.

Totally agree with that. Coming from a few backend-heavy years, I also appreciate keeping checks lean where it makes sense. In some quick validations, I’ve used JavaScript’s truthy/falsy evaluation – but with a caveat. It checks if JavaScript is not null, undefined, empty string, 0, or false. So this works:"

if (data) {
  // do something
}

But heads-up: if 0 or false are valid values in your context, this might cause silent issues. So yeah—great for one-liners, but in team projects or form inputs, I still lean on that utility method or explicit checks.