Explain why 1.0: 1 is true in js.

Why is 1.01m true in js

Mar.23,2021

The simple explanation of

is that = first compare the type , and then compare the value .

because there are no integers and floating point numbers in js, there are only number types.

typeof 1.0;  // "number"
typeof 1;    // "number"

so the types of 1.0 and 1 are the same.

then compare the value , which is obviously the same.

< hr >

the complicated explanation is to look at the specification:

  1. if the result of Type (x) is inconsistent with that of Type (y) , return false , otherwise
  2. if Type (x) result is Undefined , return true
  3. if Type (x) result is Null , return true
  4. if Type (x) result is Number , then

    1. if x is NaN , return false
    2. if y is NaN , return false
    3. if x and y are the same number, return true
    4. if x is + 0 , y is -0 , return true
    5. if x is -0 , y is + 0 , return true
    6. returns false
  5. if Type (x) result is String , if x and y are exactly the same character sequence (same length and same character corresponding to the same position), return true , otherwise, return false
  6. if Type (x) result is Boolean , if x and y are both true or false , then return true , otherwise, return false
  7. if x and y refer to the same Object object, return true , otherwise, return false

I bolstered the important paragraphs.

Within

JavaScript, all numbers are stored as 64-bit floating-point numbers, even if integers


because js does not have float , int , only number

Menu