Equals, Equals Equals, or Equals Equals Equals (=,==,=== Operators) in Java Script

I am sure that you have used the equals sign in your javascript code since it is just about impossible to write any meaningful code without it. Whenever you want to set a variable to some particular value you use an equals like this:

var x = y + z;

In this particular example we have three variables x, y, and z and we either add y and z together (if they both contain numbers) or concatenate their values together (if either of them are strings) and save the result in x. Rather trivial and does exactly what anyone who has been programming with Javascript for more than a few minutes expects. Here’s another example:

if (x = y + z) {alert(‘true’);}

This is in fact the same example as before except that we have combined it with an if statement. Again y and z are added together or concatenated and the result is saved in x for later use. Where this differs from our first example is that the sum or concatenation of y and z is also evaluated to see whether it is true or false (with false, zero and an empty string all being considered to be false while all other numbers and strings are considered to be true. Unless y + z equals 0 or “” the if statement evaluates to true and the alert is run.

A very different situation applies if you add a second equals sign into our if statement (and one which you are more likely to expect.

if (x == y + z) {alert(‘true’);}

No longer is the result of adding y and z together saved in x. Instead the result is compared to x and if they are considered to be equal then the if statement evaluates to true and the alert is run. If x is not equal to y + z then the comparison evaluates to false.

What values are considered to be equal when you compare them though. Let’s say that x is astring containing “3”, y is 5 and z is minus 2. Since 5 + -2 gives 3 the comparison is between a string containing three and a number containing three. In order to properly compare the two fields the number first gets converted to a string so as to give two strings to be compared. Since two strings both of which contain a three are the same the comparison evaluates to true.

Suppose though that we don’t want to allow a string containing a three to match with a number three. Lat’s add another equals.

if (x === y + z) {alert(‘true’);}

This still does a comparison between x and the result of adding y and z but now no conversion of numbers into strings (in fact no type conversions at all) is permitted when doing the actual comparison. Now the values being compared must be of the same data type as well as have the same value. A string containing a three is not of the same type as a number containing a three and so the comparison evaluates as false.

Note that all three of the above if statements are perfectly valid Javascript code, they just do different things. The difference between using two or three equals signs only makes a difference where the values being compared can be either the same or different data types. This means that omitting the third equals will not normally make a difference to the evaluated result making it harder to run a test that will show up the difference. Leaving out the second equals makes a much bigger difference to how the statement works. This means that were your if statement doesn’t work most of the time then a likely cause is the missing second equals.

To minimize the chance of your code not working through a missing equals sign you should always use three equals signs for a comparison unless you specifically wish to allow data conversion. Even then you are better off including the code that deliberately converts the data type rather than allowing it to happen automatically. So if x is a string and y and z are numbers we can make sure that our comparison works as well as make it clear that the right number of equals are present by using:

if (x === String(y + z)) {alert(‘true’);}

Another way of easily detecting whether we have left out an equals where one of the values to be compared is a constant is to place the constant value on the left.

if (3 === y + z) {alert(‘true’);}

The above statement will still work if one of the equals is left out and unless one or other of y and z is a string containing a three and the other is an empty string the same result will be obtained if we accidentally leave out one of the equals. If we leave out two equals this statement generates a syntax error since 3 is not a variable and we therefore can’t assign a value to it.

If we really want an assignment statement then lets make two statements out of it instead of one so as to make it clearer to anyone else reading the code (or ourselves at a later date) that the single equals is intended to be an assignment statement and not a comparison that is missing an equals.

x = y + z; if (x) {alert(‘true’);}

Now it is perfectly clear that we are assigning the result of adding y and z to x and then testing if that result is true rather than that we were trying to compare x with y+z. The code isn’t even all that much longer than if we coded it the other way.

Writing statements that contain one two or even three equals signs can all be valid statements. How you use them is what will lead to either easy to maintain code or a nightmare of trying to analyse how the code is meant to work in order to determine if a second or third equals sign is really missing from a statement or not.