Loading...

!== null vs !== undefined in Typescript or Javascript, how to check for both at once?

question typescript javascript
Ram Patra Published on November 14, 2024

The choice between !== undefined and !== null depends on the context and what you’re trying to check.

Key Differences:

  1. undefined:
    • A variable or property that has been declared but not assigned a value.
    • A function parameter that was not provided when the function was called.
    • A property that doesn’t exist in an object.
  2. null:
    • An explicit value that represents “no value” or “empty.”
    • Often used to intentionally signify the absence of a value.

When to Use:

  1. Use !== undefined when:
    • You’re dealing with cases where a variable might be uninitialized or omitted.
    • Checking whether a parameter was passed to a function.
    if (value !== undefined) {
      // Do something
    }
    
  2. Use !== null when:
    • You explicitly assign null to indicate “no value.”
    • Checking against a value explicitly set as null.
    if (value !== null) {
      // Do something
    }
    
  3. Use both (value != null) when:
    • You want to check for both null and undefined together, since null and undefined are considered equal in loose equality (==).
    • Commonly used when you don’t care about the specific type of “empty.”
    if (value != null) {
      // Do something if value is not null or undefined
    }
    

Best Practice:

  • Prefer strict equality checks (!== or ===) for better type safety and clarity.
  • Use != null only when you explicitly want to check for both null and undefined.

For your example:

  • offerPrice !== undefined is correct if you’re checking whether the parameter was passed.
  • If offerPrice might explicitly be null, you should use offerPrice != null to handle both cases.
Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on November 14, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question typescript July 20, 2024 How to filter an Array based on a condition in Typescript?

Filtering an array based on a condition in TypeScript is straightforward and similar to how you would do it in JavaScript. TypeScript adds type safety to the process, ensuring that your code is more robust and less error-prone.

question typescript July 20, 2024 How to filter a Map based on a condition in Typescript?

In TypeScript, you can filter a Map based on a condition using the Map’s built-in methods along with some of JavaScript’s array methods. Since Map is iterable, you can convert it to an array, apply the filter, and then convert it back to a Map. Here’s how you can do it:

question typescript November 21, 2024 How to parse string to boolean in Typescript?

In TypeScript, parsing a string to a boolean typically involves converting specific string values (e.g., "true" or "false") to their corresponding boolean values. Here’s how you can do it: