Archive for May 8, 2007

JavaScript Purpose of Equal Signs: Assignment, Equality and Strict Comparison

Posted in JavaScript, Software, Web, internet, programming on May 8, 2007 by Joey

There are three different ways in which equal signs can be used in JavaScript:

1. = is the assignment operator
2. == is the equality operator
3. === is the strict comparison operator

These are each described in details as follows:

1. The assignment operator utilizes one equal sign (=). It is used to assign a value to a variable, as in x=1.

2. The equality operator makes use of two successive equal signs (==). It is used to determine if two operands are equal to each other.

var intTest = 1;
document.write(intTest=='1');

The preceding example returns true.

3. The strict comparison operator consists of three consecutive equal signs (===) and is used to determine if the operands are equal while also being of the same type.

var intTest = 1;
document.write(intTest==='1');

The preceding example returns false because a number is being compared to a string, even though they are both 1.