0

I've met a problem in learning javascript.Here is my code:

var arr = [];
for (let i = 0; i <4294967296; i=i+1){
    arr[i] = 1;}

But both in chrome and firefox,it ended up with something wrong.I know that the max length of an array is 4294967295(2^32-1) in javascript,when i changed 4294967296 to 4294967295, 4294967294 , or a little more smaller,it still didn't work.I also tested it in Node.js,after a while of executing,it just ended with:

FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory

I don't know why?

buggo
  • 11
  • 1
  • 3

1 Answers1

0

1 is an integer and is 4 bytes or 8 bytes long in memory (depending on your OS), so you must not have 4M * this length bytes available in memory.