Difference between “ == “ and “ === “ operators in JS?

Forums JavaScriptDifference between “ == “ and “ === “ operators in JS?
Staff asked 3 weeks ago

Answers (2)

Add Answer
Nayan Raval Marked As Accepted
Staff answered 3 weeks ago

Both of these operators are comparison operators. The distinction between the two operators is that “==” compares values, whereas ” === ” compares both values and types.

Example:

var x = 2;
var y = "2";
(x == y)  // Returns true since the value of both x and y is the same

(x === y) // Returns false since the typeof x is "number" and typeof y is "string"

 

Staff answered 3 weeks ago

== in JavaScript is used for comparing two variables, but it ignores the datatype of variable.

=== is used for comparing two variables, but this operator also checks datatype and compares two values

Subscribe

Select Categories