Javascript best practices - Shortify falsy / truthy checks

Sometimes we need to assign only truthy (not undefined/null/empty) values to our variables.

It is done like this:


if (myVar !== null && myVar !== 'undefined' && myVar !== '') {
   yourVar = myVar;
}
else {
  yourVar = 'new default value';
}

This can be optimized as:

yourVar = myVar || 'new default value';



Nordible is a digital solutions provider helping organizations achieve their digital goals.

We develop and measure digital solutions, implement impactful brand promotion and SEO strategies and develop software driven by rigorous testing that help your business succeed online and beyond.

You can contact us at info@nordible.com for business inquiries.

Follow us: