Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Comparing Primitive Values:
4.2.
JavaScript
4.3.
Comparing Different Types:
4.4.
JavaScript
4.5.
Checking Equality of Objects:
4.6.
JavaScript
4.7.
Equality Check with Arrays:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How is _.eq() different from JavaScript's === operator?
5.2.
Does _.eq() work with complex objects like Date or RegExp?
5.3.
Can _.eq() be used for deep equality checks?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.eq() Method

Author Pallavi singh
0 upvote
Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

In programming, comparing two values for equality is a fundamental operation. Lodash enhances this functionality with its _.eq() method, which provides a straightforward way to check if two values are equal. 

Lodash _.eq() Method

This method is particularly useful for its strict comparison capabilities, ensuring that the values compared are indeed identical in both type and value.

Why This Function is Used

The _.eq() function is used for strict equality comparison of two values. Unlike the loose equality (==) in JavaScript, which performs type coercion, _.eq() compares both the type and the value, similar to the strict equality operator (===). This method is essential in scenarios where precise comparison is crucial, ensuring that only values of the same type and exact value are considered equal.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax: 

_.eq(value, other)

Parameters:

  • value: The value to compare.
     
  • other: The other value to compare.

Return Value: 

(boolean) - Returns true if the values are equivalent, else false.

Examples 

Comparing Primitive Values:

  • JavaScript

JavaScript

var _ = require('lodash');

console.log(_.eq(1, 1));

console.log(_.eq(1, '1'));

Output: 

true
false


Demonstrates the comparison of primitive values, emphasizing type equality.

Comparing Different Types:

  • JavaScript

JavaScript

console.log(_.eq('hello', 'hello'));

console.log(_.eq('hello', ['hello']));

Output: 

true
false


Shows that values of different types are not considered equal.

Checking Equality of Objects:

  • JavaScript

JavaScript

var object = { 'a': 1 };

var other = { 'a': 1 };

console.log(_.eq(object, object));

console.log(_.eq(object, other));

Output: 

true
false


An example of object comparison, highlighting that different instances are not equal, even with identical properties.

Equality Check with Arrays:

  • JavaScript

JavaScript

var array = [1, 2, 3];

var otherArray = [1, 2, 3];

console.log(_.eq(array, array));

console.log(_.eq(array, otherArray));

Output:

true
false


Demonstrates that only the same array instance is considered equal, not arrays with identical elements.

Frequently Asked Questions

How is _.eq() different from JavaScript's === operator?

_.eq() and the === operator function similarly in terms of strict equality checking. The primary difference is that _.eq() can handle certain edge cases and is consistent across different JavaScript environments.

Does _.eq() work with complex objects like Date or RegExp?

Yes, _.eq() works with complex objects. However, for these objects, the equality is based on the reference, not the content. Two different instances with the same content are not considered equal.

Can _.eq() be used for deep equality checks?

No, _.eq() performs a strict equality check, which is not suitable for deep equality. For deep equality checks, Lodash provides the _.isEqual() method.

Conclusion

Lodash's _.eq() method offers a reliable and consistent way to perform strict equality checks in JavaScript. It ensures precise comparison of values, making it a useful tool for situations where exact equivalence is necessary.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.