spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column26


The Strict Equality Operators

Developer News
ActiveState Debuts Open Source Business Suite
Salesforce Offers Visual App Builder
Codesion Steps Out From CVS's Shadow

JavaScript 1.3 introduces two new operators, the strict equality operators. The strict equality operators (=== and !==) perform equality comparisons on operands of the same type. No type conversion is performed before the comparison. They should be both Integer, String, or any other JavaScript type:

operand1 === operand2
operand1 !== operand2

Use the strict equality operator (===) when you want to check that the two operands are of the same type and value. Use the regular equality operator (==) if you care only about the value and the type does not matter. If, for example, one operand is the number 5 and the other operand is the string "5", standard equality operator will return true, but, since they are not of the same type, a strict equality operator will return false.

Comparing two operands may be tricky at times. Comparing numbers is probably the most straightforward. Numbers are equal when they are numerically equal, or to put it differently, the difference between them is zero. Strings are compared according to a standard lexicographical ordering, using Unicode values (see our previous column, JavaScript 1.3 Overview, Part I). Therefore, two strings are equal when they have the same length, same sequence of characters, exactly in the same corresponding positions. The number NaN (see our previous column) is not equal to anything, including itself. Positive and negative zeros are equal.

Two objects are equal if they refer to the exact same Object. In the assembly line example from our previous page, we use the following line to construct the volvoInterior object:

volvoInterior = new interior("blue", "leather", true);

If we duplicate this line to construct two seemingly-identical objects, volvoInterior1 and volvoInterior2:

volvoInterior1 = new interior("blue", "leather", true);
volvoInterior2 = new interior("blue", "leather", true);

a strict equality operator:

if (volvoInterior1 === volvoInterior2)

will yield false. Only when objects are identical, will the strict equality operator return true:

if (volvoInterior1 === volvoInterior1)

But this behavior of object comparison is valid for standard equality operator as well. The comparison:

if (volvoInterior1 == volvoInterior2)

returns false, while the following operation:

if (volvoInterior1 == volvoInterior1)

returns true.

Comparing a string with a number highlights the difference between strict equality and standard equality operators.

if (5 === "5")

returns false, while the following test:

if (5 == "5")

returns true.

Boolean operands are equal if they are both true or false.

http://www.internet.com


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Use Web Caching to Make Your Web Site Faster · Creating an Online Shopping Cart Mechanism in PHP · Log JavaScript Errors Using an AJAX-driven Web Service
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Configuring Granular Settings for a Database Level Audit · The Perils of a Web 2.0 Transition on Your Business Processes · Facebook Redesigns Site —Again — Nears 400M Mark


Created: September 28, 1998
Revised: September 28, 1998

URL: http://www.webreference.com/js/column26/stricteq.html