Making Comparisons in JavaScript

Comparisons are fundamental operations in JavaScript that allow you to evaluate the relationship between two values. Whether you're comparing numbers, strings, or objects, understanding how comparisons work is crucial for writing effective JavaScript code. In this chapter, we'll explore the basics of making comparisons in JavaScript, covering everything from simple equality checks to more complex comparison operators.

Equality and Identity Operators

JavaScript provides several operators for comparing values, including equality and identity operators

== (Equality Operator):

Checks if two values are equal, performing type coercion if necessary.

				
					console.log(5 == '5'); // Output: true
console.log(5 == 5); // Output: true
console.log(5 == 10); // Output: false

				
			

=== (Identity Operator):

Checks if two values are equal without type coercion (strict equality).

				
					console.log(5 === '5'); // Output: false
console.log(5 === 5); // Output: true
console.log(5 === 10); // Output: false

				
			

Comparison Operators

JavaScript also provides comparison operators for comparing numeric values.

(Greater Than Operator):

Checks if the left operand is greater than the right operand.

				
					console.log(5 > 3); // Output: true
console.log(5 > 10); // Output: false

				
			

< (Less Than Operator):

Checks if the left operand is less than the right operand.

				
					console.log(5 < 10); // Output: true
console.log(10 < 5); // Output: false

				
			

 >= (Greater Than or Equal To Operator):

Checks if the left operand is greater than or equal to the right operand.

				
					console.log(5 >= 5); // Output: true
console.log(5 >= 10); // Output: false

				
			

 <= (Less Than or Equal To Operator):

Checks if the left operand is less than or equal to the right operand.

				
					console.log(5 <= 10); // Output: true
console.log(10 <= 5); // Output: false

				
			

Logical Operators

Logical operators allow you to combine multiple comparison expressions.

 && (Logical AND Operator):

Returns true if both operands are true.

				
					console.log(5 > 3 && 10 > 5); // Output: true
console.log(5 > 3 && 10 < 5); // Output: false

				
			

|| (Logical OR Operator):

Returns true if either operand is true.

				
					console.log(5 > 3 || 10 < 5); // Output: true
console.log(5 < 3 || 10 < 5); // Output: false

				
			

Comparison with Objects

When comparing objects, JavaScript checks for reference equality by default.

				
					const obj1 = { name: 'John' };
const obj2 = { name: 'John' };
console.log(obj1 === obj2); // Output: false (different references)

				
			

Understanding how to make comparisons in JavaScript is essential for writing effective and reliable code. By mastering the various comparison operators and understanding their behavior, you'll be able to write code that accurately evaluates conditions and makes informed decisions. Experiment with different comparison scenarios and logical combinations to deepen your understanding of comparisons in JavaScript. With practice and exploration, you'll become proficient in making comparisons effectively in JavaScript. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India