Reputation: 17858
I notice that strict equal
operator returns true if the operands are equal and of the same type.
But, if I do this
01000 == 512; // returns true
01000 === 512; // returns true
or
0o535 == 349; // returns true
0o535 === 349; // returns true
Do they have the same value and type?
Upvotes: 0
Views: 213
Reputation: 1074148
Because 01000
, 512
, 0o535
, and 349
are all numbers. 01000
and 512
are the same value written different ways (a "legacy" octal literal and a decimal literal); so are 0o535
and 349
(a new-style octal literal and a decimal literal). The form of literal you use makes no difference to the value or type of what it creates. Similarly, 'foo' === "foo"
is true, even though I use the single-quoted string literal in one and the double-quoted string literal in the other.
About the two kinds of octal literals:
01000
is a "legacy" octal literal, indicated by just the leading zero. JavaScript engines in web browsers are required (as of ES2015) to support them in loose mode, and to not support them in strict mode (e.g., "use strict")
. (Note that loose mode and strict mode have nothing to do with loose equality and strict equality, which is a separate concept.) So in a compliant, browser-based JavaScript engine, 01000 === 512
is true in loose mode, but an error in strict mode, because legacy octal literals are not allowed in strict mode. (Prior to ES2015, supporting legacy octal literals in loose mode was not required.)
0o535
is the newer octal notation added in ES2015, indicated with the leading 0o
. It's supported in both loose and strict modes by compliant JavaScript engines. But again, it's new, so older browsers won't be compliant.
Upvotes: 4
Reputation: 2456
This is an interesting question because one may assume that ===
should be able to distinguish between different notations. However, this is not the case because JavaScript automatically interprets integers of different notations.
There are various numeric literals available. New notations for binary, octal, and hexadecimal are coming in from ES6.
1) decimal numbers are usually seen without a leading 0, but they can be written with one. If the next digit after the leading 0 is 8 or greater, it is parsed as a decimal. Otherwise, it is parsed as an octal.
0800 === 800
--> decimal
0700 === 448
--> octal
0700 === 0o0700
Now for the new ES6 number features:
2) binary numbers have a leading 0b
or 0B
.
3) octal numbers have a leading 0o
or 0O
.
4) hexadecimal numbers have a leading 0x
or 0X
.
Upvotes: 1
Reputation: 571
The numbers you wrote (01000 and 0o535) are just octal representations of the numbers 512 and 349.
01000 = 0*8^0 +0*8^1 + 0*8^2 + 1*8^3 = 512
0o535 = 5*8^0 + 3*8^1 +5*8^2 = 5 + 24 + 320 = 349
javascript doesn't really care about the base you use to write your numbers.
Upvotes: 0
Reputation: 21380
Because they are equal and of the same type. They both are numbers. Number does not have anything like base - it's just a representation.
Upvotes: 1
Reputation: 133
acturally, they have the same type :
typeof(01000) //"number"
typeof(0o535) //"number"
they are base 8 thought by js
Upvotes: 0
Reputation: 1125
Those are different ways of representing numbers and they're all of type "number". As an example
typeof 01000; //=> "number"
var num = 0o535;
num; //=> 349
Upvotes: 1
Reputation: 36703
If a number starts with 0
, it is interpreted as octal
, or base 8
.
01000 in base 8 is 512 in base 10.
So they are equating to true. Because the value is anyway same.
Upvotes: 2