
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Object comparison Complexity in JavaScript using comparison operator or JSON.stringlify()?
Let’s say the following are our objects −
var object1 = { firstName: "David" }; var object2 = { firstName: "David" };
You will not get the correct result using comparison operator (== or ===). Use JSON.stringify() for this.
Example
Following is the code implementing both the ways and showing the correct result −
var object1 = { firstName: "David" }; var object2 = { firstName: "David" }; if (object1 == object2) console.log("using == operator result ==> true"); else console.log("using == operator result ==> false"); if (JSON.stringify(object1) == JSON.stringify(object2)) console.log("using JSON.stringify() operator result ==> true"); else console.log("using JSON.stringify() operator result ==> false");
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo224.js.
Output
The output is as follows −
PS C:\Users\Amit\JavaScript-code> node demo224.js using == operator result ==> false using JSON.stringify() operator result ==> true
- Related Articles
- Perform element-wise comparison of two string arrays using a comparison operator in Numpy
- Comparison between "&&" and "AND" operator in PHP.
- Python Object Comparison “is” vs “==”
- object.is() in equality comparison JavaScript
- What are Comparison Operators in JavaScript?
- How does comparison operator work with date values in MySQL?
- How to use comparison operator for numeric string in MySQL?
- Comparison of autoboxed integer object in Java\n
- Explain Strict Comparison in JavaScript switch statement?
- String Comparison in Java
- Comparison between E-R Model and Object Oriented Model
- Python Comparison Operators
- Degree of Comparison
- What is MySQL NULL-safe equal operator and how it is different from comparison operator?
- How to do case-sensitive string comparison in JavaScript?

Advertisements