1

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?

choz
  • 17,242
  • 4
  • 53
  • 73
  • `typeof(0o535) === typeof(349)` you compare different number notations.. – Hacketo Dec 15 '15 at 08:36
  • 1
    Possible duplicate of [Javascript === (triple equals)](http://stackoverflow.com/questions/5112934/javascript-triple-equals) – Florian Neumann Dec 15 '15 at 08:37
  • They do have the same value (512), and type (number). If you precede the number with `0`, you are expressing the number in octal system; but it is the same number however you represent it. – Amadan Dec 15 '15 at 08:39
  • @Hacketo I don't think those codes represent as `'number' === 'number'`. – choz Dec 15 '15 at 08:39
  • 1
    Try doing `parseInt(01000,10)` and you'll get `512`, the base is different – adeneo Dec 15 '15 at 08:39
  • @choz "Do they have the same type" , these are number. – Hacketo Dec 15 '15 at 08:40

7 Answers7

4

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @Qwertiy: You're quite right, I keep forgetting that change in ES2015 (up through ES5, no, they didn't). I'll fix it, thank you! – T.J. Crowder Dec 15 '15 at 08:55
2

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.

void
  • 36,090
  • 8
  • 62
  • 107
1

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
Sam
  • 1,115
  • 1
  • 8
  • 23
1

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.

Qwertiy
  • 19,681
  • 15
  • 61
  • 128
1

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.

boombox
  • 2,396
  • 2
  • 11
  • 15
0

acturally, they have the same type :

typeof(01000) //"number"
typeof(0o535) //"number"

they are base 8 thought by js

bjwzds
  • 103
  • 1
  • 1
  • 5
0

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.

Or Yaniv
  • 571
  • 4
  • 11