ArticleZip > How To Get Value From Object With Default Value

How To Get Value From Object With Default Value

When working with objects in programming, there comes a time when you need to access a property's value, but you also want to ensure that if the property is undefined or null, a default value is returned instead. This practice is quite common in software development and can save you from potential headaches down the line. In this article, we will walk you through how to get value from an object with a default value in JavaScript, a powerful yet commonly used programming language.

First things first, let's understand the scenario. You have an object, and you want to retrieve the value of a specific property. However, if that property does not exist or is set to null or undefined, you would like to have a fallback default value returned instead.

One way to achieve this is by using the logical OR operator (||). This operator allows you to provide a default value if the left side of the expression is falsy, which includes null, undefined, 0, an empty string, and NaN. Here's an example to illustrate how this works:

Javascript

const obj = {
  key: null,
};

const value = obj.key || 'default';
console.log(value); // Output: 'default'

In the code snippet above, we are trying to access the value of the `key` property in the `obj` object. Since `obj.key` is set to null, the expression `obj.key || 'default'` evaluates to 'default', and that is what gets assigned to the `value` variable.

While the logical OR operator works well for simple cases, it has a limitation when the property value itself could be a falsy value. In such cases, you can leverage the ternary operator to handle more complex scenarios. Here's how you can use it:

Javascript

const obj = {
  key: 0,
};

const value = obj.key !== undefined && obj.key !== null ? obj.key : 'default';
console.log(value); // Output: 0

In this example, we first check if the `key` property exists and is not null or undefined. If that condition is met, we assign the actual value of `obj.key` to the `value` variable; otherwise, we fallback to the default value 'default'.

Another approach to achieving the same result is by using the `Object.hasOwnProperty()` method, which checks if an object has the specified property as its own property. This method can be quite handy when dealing with nested objects. Here's an example:

Javascript

const obj = {
  nested: {
    key: 'value',
  },
};

const value = obj.nested.hasOwnProperty('key') ? obj.nested.key : 'default';
console.log(value); // Output: 'value'

In this scenario, we first check if the `nested` object has the property `key`. If it does, we assign the value of `obj.nested.key` to `value`; otherwise, we fallback to the default value 'default'.

By implementing these techniques, you can safely retrieve values from objects in JavaScript while ensuring that default values are provided when needed. This practice not only enhances the robustness of your code but also improves its readability and maintainability. Happy coding!

×