Skip to content

new Array(2 ** 32) makes let and const declarations that can't be initialized. #53356

Closed as not planned
@1Git2Clone

Description

@1Git2Clone

Version

v22.2.0

Platform

Linux hutao 6.9.3-arch1-1 #1 SMP PREEMPT_DYNAMIC Fri, 31 May 2024 15:14:45 +0000 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

 node
Welcome to Node.js v22.2.0.
Type ".help" for more information.
let x = new Array(2 ** 32); // or const, also works the same, `var` is not affected.

How often does it reproduce? Is there a required condition?

Consistently reproducable via making a variable which stores a new Array that has a length higher than the limit (2^32 - 1).

What is the expected behavior? Why is that the expected behavior?

The same way the chromium developer console handles it by disallowing the declaration of the variable all together.

let x = new Array(2**32);
Uncaught RangeError: Invalid array length
    at <anonymous>:1:9
let x = 1;
undefined
console.log(x);
1
undefined

I mean it only makes sense, especially in const where it'd be weird to have a constant that's not defined (despite it being in JavaScript).

Although to be honest this is also an issue with const values even in Firefox...

Chromium - handles it correctly because it's not allowing for undefined const values

const y = new Array(2**32);
Uncaught RangeError: Invalid array length
    at <anonymous>:1:11
typeof(y)
Uncaught ReferenceError: y is not defined
    at <anonymous>:1:1
const y = 1;
undefined
typeof(y)
'number'

Firefox - how can you have an undefined const?

const x = new Array(2**32);
Uncaught RangeError: invalid array length
    <anonymous> debugger eval code:1
const x = 1;
Uncaught SyntaxError: redeclaration of const x
    <anonymous> debugger eval code:1
typeof(x)
"undefined" 

What do you see instead?

 node
Welcome to Node.js v22.2.0.
Type ".help" for more information.
> let x = new Array(2 ** 32);
Uncaught RangeError: Invalid array length
> console.log(x)
Uncaught ReferenceError: x is not defined
> x = 1
Uncaught ReferenceError: Cannot access 'x' before initialization
> console.log(x)
Uncaught ReferenceError: x is not defined
> typeof(x)
Uncaught ReferenceError: x is not defined
> let x = 1
Uncaught SyntaxError: Identifier 'x' has already been declared
> var y = new Array(2**32);
Uncaught RangeError: Invalid array length
> const z = new Array(2**32);
Uncaught RangeError: Invalid array length
> typeof(y)
'undefined'
> typeof(z)
Uncaught ReferenceError: z is not defined
> console.log(y)
undefined
undefined
> console.log(y)
undefined
undefined
> console.log(z)
Uncaught ReferenceError: z is not defined

Undefined const variable: z as well as a declared (but inaccessible) let variable: x.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateIssues and PRs that are duplicates of other issues or PRs.replIssues and PRs related to the REPL subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions