Replace lodash.get method with optional chaining and nullish-coalescing operator
Author: Sanjib Roy
Published on: March 2, 2023
This blog is also published in blog.saeloun.com
Lodash is a well-known JavaScript library that simplifies accessing nested object properties using its get method. However, get method has some limitations, hich can slow down our code. Here, we will discuss how we can use ES11’s optional chaining and nullish-coalescing operators to simplify our code and avoid some of the limitations of lodash.get.
Why we preferred lodash.get earlier?
We used lodash.get earlier as it’s a popular utility method for accessing properties of nested objects. The get method is less prone to errors and offers features such as default values and support for accessing properties using dot notation or array notation.
Limitations of using lodash.get
The lodash.get method is a powerful tool that can help access deeply nested values in JavaScript objects. However, it has some limitations:
-
It can be slower than other methods of accessing nested values, especially when dealing with large objects.
-
It requires the entire lodash library, which can significantly increase the size of our codebase.
-
It doesn’t work with some types of objects, such as arrays and null values.
- It can be difficult to read and understand when used with deeply nested objects. On the other hand, optional chaining offers a simpler syntax that looks like accessing properties directly on an object, making it easier to read and understand.
Optional Chaining
The Optional chaining is a new syntax introduced in ES11(ES2020)
that allows us to safely access deeply nested properties without causing errors.
With this syntax, if the object is null
or undefined
,
the expression will return undefined
,
instead of throwing an error.
Nullish-Coalescing Operator
The Nullish-Coalescing Operator is another new feature in JavaScript introduced in ES11(ES2020) that lets us set a default value for variables
that are either undefined
or null
.
This can be helpful when we want to ensure that a variable has a specific value,
even if the original value is falsy
.
We can also use this operator to set a default value for a function parameter -
Replicating lodash.get default value functionality with Optional Chaining and Nullish-Coalescing
While optional chaining is useful, it doesn’t offer the same default value functionality as lodash.get. However, we can replicate this behavior by combining optional chaining with the nullish-coalescing operator.
Conclusion
Lodash is indeed a popular JavaScript library with a lot of useful functions. However, using it solely for the get method might not be the best approach as it would result in adding an unnecessary dependency to your project. Additionally, if you only need to retrieve a single value from an object and provide a default value if the property is absent, it may be more efficient to use JavaScript’s native optional chaining(?.) and nullish-coalescing(??) operators.