# Strict equality

``````if(123===123.0){
}
else{
}
``````

why this code return true ?

Because the number 123 is exactly the same as the number 123.0 (or 123.00, or 123.000, you get the idea).

Strict equality means that JS won’t use coercion to test for equality. It doesn’t mean that the two numbers have to be written exactly the same in order to be considered equal. For example:

``````10/5 === 2
``````

is going to give you `true` as well.

3 Likes

Unlike many other languages, javascript doesn’t see a difference between an integer and a floating-point number. They’re simply both numbers.

Strict equality won’t pass with `123==="123"` as they are different types, and strict equality does not allow conversion between types (called “coercion”).

Now I’m wondering, though (and I’m on my phone so i can’t test) does `Number(123)===BigInt(123)`?

@snowmonkey No BigInt and Number are not the same type.

``````0n === 0
// false

0n == 0
// true
``````

As said, strict equality deals with the type. ECMAScript has two built-in numeric types: Number and BigInt. An Integer and a Float is the same type (Number), the equality checks the values. There can be some funkiness caused by imprecision.

``````100 === 100.00000000000001
false

100 === 100.000000000000001
true
``````

If you want to check that a value is an Integer you can use a method like isInteger.

Some more random console logs:

``````-0 === +0
// true

100000000 === 100_000_000
// true

1000000000 === 1e9
// true
``````

Numbers

1 Like

Right, 123.0 gets stored exactly the same (in JS). For example:

``````console.log(123)
// 123
console.log(123.0)
// 123
console.log(123.00)
// 123
``````

JS doesn’t store trailing decimal zeroes. I assume that it just stores all numbers as a float.

Thank you! I actually read a great post by J.R.Sinclair about a writing a custom math library using BigInt, as a means of avoiding the floating point issue, and as i recall that was one of my main sticking points - in order to use them with regular number values, one or the other must be coerced.

Fun topic, but the gist is that js doesn’t distinguish between ints and floats.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.