← Back to list

18 JavaScript Code Snippets for Handling null, NaN, and undefined

Null, NaN, and undefined are common values that programmers encounter while working with JavaScript.

Evelyn Taylor · 2023-06-26 13:36 · 1 claps · 3.1 min read paywalled
#handling-missing-values #handling-null #handling-undefined #handling-nan #javascript-tips
Open on Medium ↗
Wiki topics: CRY · Crypto & Web3 🌐 · Web Development 🐾 · Pets & Animals

18 JavaScript Code Snippets for Handling null, NaN, and undefined

Null, NaN, and undefined are common values that programmers encounter while working with JavaScript.

Handling these values effectively is crucial to ensure the stability and reliability of your code.

In this article, we will explore 18 JavaScript code snippets that provide handy solutions for dealing with null, NaN, and undefined scenarios.

These code snippets will help you write cleaner and more robust code by effectively handling these values.

1. Checking for null:

To check if a variable is null, you can use the strict equality operator (===) to compare it directly with null:

if (variable === null) {
  // Code to handle null value
}

2. Checking for undefined:

Similarly, you can check if a variable is undefined using the typeof operator:

if (typeof variable === 'undefined') {
  // Code to handle undefined value
}

3. Checking for NaN:

To check if a value is NaN (Not-a-Number), you can use the isNaN() function:

if (isNaN(value)) {
  // Code to handle NaN value
}

4. Defaulting to a value if null or undefined:

You can use the logical OR operator (||) to provide a default value if a variable is null or undefined:

const result = variable || defaultValue;

5. Defaulting to a value if NaN:

If a value is NaN, you can use the isNaN() function along with the logical OR operator to provide a default value:

const result = isNaN(value) ? defaultValue : value;

6. Converting null or undefined to an empty string:

To convert a null or undefined value to an empty string, you can use the logical OR operator and an empty string:

const result = variable || '';

7. Converting null or undefined to zero:

If you need to convert null or undefined to zero, you can use the logical OR operator and the number zero:

const result = variable || 0;

8. Converting null or undefined to a default object:

To convert null or undefined to a default object, you can use the logical OR operator with an empty object literal:

const result = variable || {};

9. Checking if a variable is null or undefined:

You can combine the null and undefined checks using the logical OR operator:

if (variable === null || typeof variable === 'undefined') {
  // Code to handle null or undefined value
}

10. Checking if a value is null, undefined, or NaN:

Combine the null, undefined, and NaN checks with the logical OR operator:

if (variable === null || typeof variable === 'undefined' || isNaN(variable)) {
  // Code to handle null, undefined, or NaN value
}

11. Short-circuit evaluation with null or undefined:

Use the logical AND operator (&&) to perform short-circuit evaluation if a variable is null or undefined:

const result = variable && someFunction();

12. Short-circuit evaluation with NaN:

If a value is NaN, you can use the logical AND operator for short-circuit evaluation:

const result = !isNaN(value) && someFunction();

13. Optional chaining:

To avoid errors when accessing properties of potentially null or undefined objects, you can use the optional chaining operator (?.):

const result = object?.property;

14. Nullish coalescing operator:

The nullish coalescing operator (??) provides a concise way to provide a default value for null or undefined variables:

const result = variable ?? defaultValue;

15. Converting null or undefined to a boolean value:

To convert null or undefined to a boolean value, you can use the logical NOT operator (!):

const result = !!variable;

16. Converting NaN to a boolean value:

To convert NaN to a boolean value, you can use the isNaN() function with the logical NOT operator:

const result = !isNaN(value);

17. Handling null or undefined in function parameters:

You can use default parameter values to handle null or undefined values in function parameters:

function myFunction(param = defaultValue) {
  // Code that uses the parameter
}

18. Removing null or undefined values from an array:

To remove null or undefined values from an array, you can use the filter() method:

const newArray = originalArray.filter((value) => value !== null && typeof value !== 'undefined');

Conclusion:

By utilizing these 18 JavaScript code snippets, you can effectively handle null, NaN, and undefined scenarios in your code.

Whether you need to check for these values, provide default values, or convert them to different types, these snippets will help you write cleaner and more robust JavaScript code.

Remember to choose the appropriate snippet based on your specific use case and ensure the reliability of your code in various scenarios.

Connect with me on Medium ✍ : **https://medium.com/@Evelyn.Taylor**


메타데이터
post_id
55ff2e8b59a3
slug
18-javascript-code-snippets-for-handling-null-nan-and-undefined-55ff2e8b59a3
url
https://medium.com/@Evelyn.Taylor/18-javascript-code-snippets-for-handling-null-nan-and-undefined-55ff2e8b59a3
canonical_url
https://medium.com/@Evelyn.Taylor/18-javascript-code-snippets-for-handling-null-nan-and-undefined-55ff2e8b59a3
author_url
https://medium.com/@Evelyn.Taylor
status
ok
fetched_at
2026-08-05 12:37:12