Why === and not =

Hello everyone

I was was just wondering why we use === equaliser instead of just =. When I was younger and programming my ZX Spectrum we used = to check if something was equal to something.

Adrian

It’s because in Javascript = is used as an assignment. (Oh and also, = is an operation.)

Example:
var x = 0;
x = 42;

Also remember:

x = 32;

The code at right above of this text returns undefined.

=== returns a boolean true/false according into is the left and right the same.

Example:
42 === 42 (returns true)
3 === ‘3’ (returns false)
var x = 55 (returns undefined)
x === 45 (returns false)

Sorry for the loooooong explanation.

1 Like

It’s because in JavaScript === and = are different. = is used to assign an identifier (variable) eg:
var x;
x = 42;
Then, === checks if two values are the same. For example:
if (1 === 1) {
print(‘Yes’);
}
if (1 === 2 === 3) {
print(‘Yes?!’);
}
I hope you have a solid understanding about === and = in JavaScript now.