Comparison

In Javascript there are 3 types we are often comparing: String, Number and Boolean. After digging through the ECMA-262 specifications, here is the behaviour of the == operator (11.9.3) on these types:

  • Number == String
    Typecasted as follow: Number == Number(String)
  • Number == Boolean
    Typecasted as follow: Number == Number(Boolean)
  • String == Boolean
    Typecasted as follow: Number(String) == Number(Boolean)

This means that when comparing data of two different types, they will first be converted to Number.

Note: The order of the equality is not important: A == B is the same as B == A (except the order of evalution of A and B).

You can force comparison of a and b with the type you want:

  • String: "" + a == "" + b
  • Number: +a == +b
  • Integer: a | 0 == b | 0
  • Boolean: !!a == !!b

Addition / Concatenation

The binary + operator follows a simple rule (11.6.1):

  • if one of the operands is a String, both operands are converted to String and the + is a concatenation.
  • Else, both operands are converted to Number and the + is an addition.

Note: Some Objects are considered as Strings like Arrays. See 8.6.2 for more informations.

Note: The operator is binary so 1 + 1 + 'a' will be executed as (1 + 1) + 'a' and therefore be equal to "2a" and not "11a".

If you liked this article, you might be interested in my Twitter feed as well.
 
 

Related Posts

  • August 29, 2011 Javascript: Improve Cache Performance: Reduce Lookups (2)
    In my Binary Decision Diagram Library, the performance bottleneck was the uniqueness cache. By reducing the number of cache lookup, it is possible to greatly improve the performances. Common pattern In order to test if the key is already in the cache, the usual pattern is to use key […]
  • November 4, 2013 Bitwise Truthiness (0)
    In this blog post, I explore another form of truthiness in Javascript. What happens if you use a bitwise operator on a value like 0|value or ~~value. Context We recently turned on the JSHint bitwise rule by default and the following code was caught. var isValid = false; for […]
  • August 25, 2009 Test – Are you a Javascript Guru? (0)
    Javascript is a very flexible language, I made a compilation of some edge cases that you may have encountered while programming. The main goal is to point out some interesting specific behaviors. Concatenation 1] var result = [10] + 1; [10, 1] "101" 11 Explanation: The Array […]
  • November 10, 2011 Diablofans Theme Improvement (0)
    Diablofans.com needed a bit of love. It is really gratifying to be able to visually improve a website by an order of magnitude just by changing some colors and fixing broken layout :) Here are some of the changes I made: Post Poll Blizzquote
  • January 17, 2010 Javascript – Ajax Binary Reader (19)
    BinaryReader is buggy and no longer maintained. Check out jDataView for an up to date version. With WebGL coming in, it is important to be able to deal with binary data files like the models. Since there is no such thing on the internet right now I decided to make my own. The […]