Decoding JavaScript Mysteries: Unraveling the Memes Behind '11'+1 and '11'-1"

Decoding JavaScript Mysteries: Unraveling the Memes Behind '11'+1 and '11'-1"

·

3 min read

We've all come across numerous memes highlighting the eccentricities of JavaScript. Many fellow bootcamp attendees I've met are firmly convinced that JavaScript possesses a mind of its own, seemingly determined to avoid throwing any errors. Join me as we explore the amusing quirks and peculiarities that make JavaScript the enigmatic language it is.

1.Why is '11'+1='111'

To unravel these mysteries, let's take a deep dive into the ECMAScript documentation.Below is the ecmascript documentation for '+' operator

12.8.3.1Runtime Semantics: Evaluation

AdditiveExpression:AdditiveExpression+MultiplicativeExpression

  1. Let lref be the result of evaluating AdditiveExpression.

  2. Let lval be ? GetValue(lref).

  3. Let rref be the result of evaluating MultiplicativeExpression.

  4. Let rval be ? GetValue(rref).

  5. Let lprim be ? ToPrimitive(lval).

  6. Let rprim be ? ToPrimitive(rval).

  7. If Type(lprim) is String or Type(rprim) is String, then

    1. Let lstr be ? ToString(lprim).

    2. Let rstr be ? ToString(rprim).

    3. Return the string-concatenation of lstr and rstr.

  8. Let lnum be ? ToNumber(lprim).

  9. Let rnum be ? ToNumber(rprim).

  10. Return the result of applying the addition operation to lnum and rnum. See the Note below 12.8.5.

In the ECMAScript documentation, the behavior of the + operator involves several abstract operations such as ToNumber(), ToString(), ToPrimitive(), and GetValue().While these abstract operations may not be directly utilized by programmers in their day-to-day coding, they serve as the behind-the-scenes mechanisms that enable the consistent and predictable behavior of JavaScript.

  1. If Type(lprim) is String or Type(rprim) is String, then

    1. Let lstr be ? ToString(lprim).

    2. Let rstr be ? ToString(rprim).

    3. Return the string-concatenation of lstr and rstr.

In the context of the + operator in JavaScript, when either the left or right variable is a string, the ToString() method is invoked on both variables. This process ensures that both operands are converted to strings before the concatenation operation takes place.

Let us breakdown '11'+1

  • The left operand is the string '11'.

  • The right operand is the number 1.

Since the left operand is a string and the right operand a number ToString() method is called upon both the operands

  1. ToString('11'): Since the operand is already a string no conversion is needed.

  2. ToString(1): The right operand is a number, so it is converted into String.

Now, the expression becomes 11 - 1, resulting in 10.

2.Let's now look at why '11'-1=10

This is Ecmascript definition for the subtraction operator

12.8.4The Subtraction Operator ( - )

12.8.4.1Runtime Semantics: Evaluation

AdditiveExpression:AdditiveExpression-MultiplicativeExpression

  1. Let lref be the result of evaluating AdditiveExpression.

  2. Let lval be ? GetValue(lref).

  3. Let rref be the result of evaluating MultiplicativeExpression.

  4. Let rval be ? GetValue(rref).

  5. Let lnum be ? ToNumber(lval).

  6. Let rnum be ? ToNumber(rval).

  7. Return the result of applying the subtraction operation to lnum and rnum. See the note below 12.8.5.

As observed earlier, when dealing with the subtraction operator in JavaScript, the abstract function ToNumber() is called upon both operands. This process ensures that both operands are converted to numeric values before the subtraction operation takes place.

Subtraction Operation: '11' - 1

Let's break down the expression '11' - 1:

  • The left operand is the string '11'.

  • The right operand is the number 1.

Since the subtraction operator involves numeric operations, both operands need to be converted to numbers. Therefore, ToNumber() is applied to both '11' and 1.

  1. ToNumber('11'): Converts the string '11' to the numeric value 11.

  2. ToNumber(1): The right operand is already a number, so no conversion is needed.

Now, the expression becomes 11 - 1, resulting in 10.

Hence, '11' - 1 equals 10. The abstract operation ToNumber() ensures consistent and predictable behavior, even when dealing with mixed data types in arithmetic expressions.

I will write more articles about Coercion and other javascript behaviors in the future.