-3

Why can't we provide size of array in JavaScript?

I mean even if it is possible why don't we why we just simply define the array.

Ivar
  • 6,138
  • 12
  • 49
  • 61
  • Can you please explain your question with the help of some code? – Rohit Agrawal Nov 20 '17 at 11:52
  • 1
    Uh? You can specify the size of an array: `var a = Array(25)` declares an array of 25 elements. – Federico klez Culloca Nov 20 '17 at 11:52
  • 1
    Take a look at [this post](https://stackoverflow.com/questions/4852017/how-to-initialize-an-arrays-length-in-javascript), it may help you I hope. Regards. – Mario Recamales Nov 20 '17 at 11:55
  • well thanks for replying but basically we don't define so that's my question why **don't** we define the size.Like in c language we need to define the size were as it's fine I js if we don't so please can you say WHY IS IT SO !! – Krypton_H4x0r Nov 20 '17 at 11:56
  • JS Arrays may be implemented as Linked Lists or special Hashmaps, or as Vector containers. It depends by the JS Engine. When you use any of this data structures, you don't have to define a size upfront – Alberto Schiabel Nov 20 '17 at 12:02

4 Answers4

2

Because standard arrays in JavaScript aren't really arrays at all (spec | post on my blog), they're just objects backed by Array.prototype with special handling for a class of property names ("array indexes"), a special length property, and a built-in literal notation. They aren't contiguous blocks of memory as in some other languages (barring optimization, of course).

I have a question in my mind about why can't we provide size of array in JavaScript ??

You can create an array with a given length via Array(n) where n is the length as a number. But again, it doesn't preallocate memory for that many slots or anything. You just end up with a sparse array with length set to n and no entries in it:

var a = Array(42);
console.log(a.length);      // 42
console.log(0 in a);        // false, it doesn't have an entry 0
a.forEach(function(entry) { // Never calls the callback
    console.log(entry);     // because the array is empty
});

I mean even if it is possible why don't we why we just simply define the array.

Because it serves no purpose.

Now, for typed arrays (Uint8Array and similar), we do indeed create them with a specific length (var a = new Uint8Array(42);), and that length is fixed (cannot change), because they're true arrays.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

You can provide size of array. If its not given, you can add multiple values dynamically.

var arr = new Array(5);
Chidambaram
  • 434
  • 4
  • 14
0
  1. You can provide size of array in java-script.
  2. Java-script array is different from array in C language. You can read more on following link understanding-javascript-arrays
AKap
  • 1
  • 1
-1

You can provide a size of an array and that size of an array will not change in the program.

Array is an object backed by Array.prototype, so there is a function called seal.

var myArray = Object.seal([5, 6, "saurabh", "text"]); // this is an array of size 4 fixed.
//myArray.push('new text'); //throw exception error
console.log(myArray[2]); //"saurabh"
myArray[0] = "change text";
console.log("print myArray: ", myArray);

You can read more over here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal

maSC0d3R
  • 339
  • 1
  • 4
  • 6