about some inconsistence of bigint

44 views
Skip to first unread message

Shang Yu

unread,
Jul 14, 2023, 3:25:14 AM7/14/23
to v8-dev
Hi guys,
Have a look at following code

let bigint = Object(1n)
console.log(1n.__proto__===bigint.__proto__)
console.log(typeof 1n)
console.log(typeof bigint)

the output is
true      // A
bigint   // B
object  // C
why while at A is true but at B is bigint (not the expected object)? or
why at C is not bigint?
If when 1n.__proto__ is evaluated, the 1n is implicitly converted to an Object, why the following code does not work?
1.__proto__    // SyntaxError: Invalid or unexpected token
Many thanks!

Shang Yu

unread,
Jul 14, 2023, 3:37:15 AM7/14/23
to v8-dev
update

Hi guys,
Have a look at following code

let bigint = Object(1n)
console.log(1n.__proto__===bigint.__proto__)
console.log(typeof 1n)
console.log(typeof bigint)

the output is
true      // A
bigint   // B
object  // C
why while at A is true but at B is bigint (not the expected object)? or
why at C is not bigint?

DELETED
If when 1n.__proto__ is evaluated, the 1n is implicitly converted to an Object, why the following code does not work?
1.__proto__    // SyntaxError: Invalid or unexpected token


Many thanks!

Jakob Kummerow

unread,
Jul 17, 2023, 4:42:14 AM7/17/23
to v8-...@googlegroups.com
typeof Object(1n) === "object" because that's what the Object constructor does: it creates an object. It's the same for other primitives, e.g. typeof Object(1) or typeof Object("foo") or typeof Object(true) are all "object".

And of course, typeof 1n === "bigint" because what else would it be?

There's nothing specific about BigInt in any of these examples, it's exactly the same for Numbers and Strings and other primitives. And AFAICT there's no inconsistency either, this is just how JavaScript works.

1.__proto__ is a SyntaxError because 1. is a Number, and you can't just write __proto__ after a Number literal. Try 1..__proto__ or, equivalently, (1).__proto__ to make it work.

Reply all
Reply to author
Forward
0 new messages