Coercion exercise

Coercion exercise for testing and practicing the knowledge learned in this module

Now that we have had a deeper look at coercion and how some of the common abstract operations work to make coercion work, let us test what we learned in this module. This following exercise will allow us to consolidate our understanding of the topic of coercion.

Following are some expressions that involve coercion. Try to guess the output based on the knowledge you gained in this module. Don't worry if you don't understand all of them. Their explanation is also given below. You can obviously refer to the specification and the earlier lessons in this module to understand and guess the output of the expressions below.

Below is an explanation of the output of each of the above-mentioned expressions.

0 == false#

Let's start with an easy one, and most people will probably get it right, even without reading this module. Now that we know how the abstract equality operator works let us understand the steps taken to evaluate this expression as true.

  1. As the types are not equal and one of the operands is a boolean, the boolean operand is converted into a number using the ToNumber abstract operation. So, the first coercive step is to convert false into a number, i.e., 0. The expression becomes:

  2. Now the types are equal, so the strict equality comparison is performed, i.e., 0 === 0, giving us true as an output.

"" == false#

  1. As the types are not equal and one of the operands is a boolean, the boolean operand is converted into a number using the ToNumber abstract operation. So, the first coercive step is to convert false into a number, i.e., 0. The expression becomes:

  2. Now, we have a string and a number. Recall that the abstract equality operator prefers number comparison, so the string operand is converted into a number using the ToNumber abstract operation. An empty string, when converted into a number, outputs 0. So the expression becomes:

  3. The types are equal, so the strict equality comparison is performed, i.e., 0 === 0, giving us true as an output.

0 == []#

  1. The array is converted into a primitive value using the ToPrimitive abstract operation. As the abstract equality operator prefers number comparison, the array is converted into a primitive value with a number as the preferred type. An empty array, when converted into a primitive value, outputs an empty string. So the expression becomes:

  2. Next, the string will be converted into a number. An empty string converted into a number outputs 0. So the expression becomes:

  3. The types are equal, so the strict equality comparison is performed, i.e., 0 === 0, giving us true as an output.

[123] == 123#

  1. We have a comparison between an array and a number. So, the array is converted into a primitive value using the ToPrimitive abstract operation, with number as the preferred type. The valueOf method will be invoked first, as the preferred type is a number. But we know that the default implementation of the valueOf method simply returns the object on which it is called. So, the toString is invoked next. For Arrays, the toString method returns an empty string for empty arrays; for an array like [1, 2, 3], it returns the contents of the array as a string, joined by commas, i.e., "1,2,3". Each array element is coerced into a string and then joined by comma(s).

    In our case, we have a single element in an array, i.e., [123], so it will be coerced into "123". So the expression becomes:

  2. Next, the string will be converted into a number. So the expression becomes:

  3. The types are equal, so the strict equality comparison is performed, i.e., 123 === 123, giving us true as an output.