-1

Does right side of operator is forced to convert to type on left?

number == string -> number == number

Are both sides converted to same underlying type, like number?

boolean == string -> number == number

Are there different rules of every operator *-/+||&&%??

I've looked at other question/answers they are unclear and confusing.

Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168
  • Does it matter what type it is converted to? what matters is that `==` normalises to comparable types. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators – Avin Kavish Oct 16 '18 at 03:14
  • it matters...@AvinKavish for example to know whether this is true or not you need to know what is going on as in what are values being converted to first. `fasle == "0"` if they are being converted to number first then it's true as `0 == 0` otherwise they are being convert to boolean then it's `false == true` as string is true – Muhammad Umer Oct 16 '18 at 03:22
  • 1
    @AvinKavish—yes, see the [*Abstract Equality Comparison* algorithm](http://ecma-international.org/ecma-262/8.0/#sec-abstract-equality-comparison). – RobG Oct 16 '18 at 03:22
  • Ok i see, it's never been a problem for me because I always use strict comparison with explicit type conversion. – Avin Kavish Oct 16 '18 at 03:26
  • that'd be ok if you always read your own code, if you are reading someone else code then you will end up in situation where `==` are used. This is also asked in interviews – Muhammad Umer Oct 16 '18 at 03:29

4 Answers4

2

The rules are given in the Abstract Equality Comparison algorithm.

Does right side of operator is forced to convert to type on left?

number == string -> number == number

Step 4: If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

So yes.

Are both sides converted to same underlying type, like number?

boolean == string -> number == number

Step 6: If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

which gives different types (number and string), so it goes to Step 4: number == ToNumber(y).

So yes again.

Are there different rules of every operator *-/+||&&%??

Other operators may do coercion of the result of evaluating the expression so I guess "Yes". Read the relevant parts of ECMA-262#expressions.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

When type conversion is involved in the comparison (i.e., non–strict comparison), JavaScript converts the types String, Number, Boolean, or Object operands as follows:

  • When comparing a number and a string, the string is converted to a number value. JavaScript attempts to convert the string numeric literal to a Number type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest Number type value.

  • If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.

  • If an object is compared with a number or string, JavaScript attempts to return the default value for the object. Operators attempt to convert the object to a primitive value, a String or Number value, using the valueOf and toString methods of the objects.

  • If this attempt to convert the object fails, a runtime error is generated. Note that an object is converted into a primitive if, and only if, its comparand is a primitive. If both operands are objects, they're compared as objects, and the equality test is true only if both refer the same object.

Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168
0

== converts the operands to the same type before making the comparison.

For relational abstract comparisons (e.g., <=), the operands are first converted to primitives, then to the same type, before comparison.

Strings are compared based on standard lexicographical ordering, using Unicode values.

Read the source for more clarity

0

http://ecma-international.org/ecma-262/8.0/#sec-abstract-equality-comparison

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is the same as Type(y), then Return the result of performing Strict Equality Comparison x === y.

  2. If x is null and y is undefined, return true. If x is undefined and y is null, return true.

  3. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

  4. If Type(x) is String and Type(y) is Number, return the result of the comparison Number(x) == y.

  5. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

  6. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

  7. If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).

  8. If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x) == y. Return false.

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168