Skip to content
Related Articles
Open in App
Not now

Related Articles

Create an array of given size in JavaScript

Improve Article
Save Article
Like Article
  • Last Updated : 05 Jan, 2023
Improve Article
Save Article
Like Article

Creating an array of a given size can be implemented in many ways, and each way is useful in some conditions. We will explain each possible ways to create an array with the given size with a proper example.

Method 1: In this method, we use the JavaScript Array constructor to create an array. It will just create an array without any value with a given size. But the values of the array will be undefined.

Example:

Javascript




<script>
    var arr = new Array(5);
    console.log(arr.length);
    console.log(arr)
</script>


Output:

5
[undefined, undefined, undefined, undefined, undefined]

Method 2: We will use apply() and map() methods to create an array with the given size. It will create an array without any value with a given size. But the values of the array will be undefined.

Example:

Javascript




<script>
    var arr = Array.apply(null, Array(5))
        .map(function () {});
          
    console.log(arr.length);
    console.log(arr);
</script>


Output:

5
[undefined, undefined, undefined, undefined, undefined]

Method 3:  We will use apply() and map() methods to create an array with the given size. This will create an array without any value with a given size. We will put the values of the indexes so it will give you an array of length 5 and its values will be the index number 0, 1, 2, 3, 4.

Example:

Javascript




<script>
    var arr = Array.apply(null, Array(5))
        .map(function (y, i) { return i; });
          
    console.log(arr.length);
    console.log(arr)
</script>


Output:

5
[0, 1, 2, 3, 4]

Method 4: In ES6, we got something new called JavaScript Array.from() function, by which we can create an array of a given size.

Example:

Javascript




<script>  
    var arr = Array.from(Array(5));
    console.log(arr.length);
    console.log(arr);
</script>


Output:

5
[undefined, undefined, undefined, undefined, undefined]

Method 5: If you want to create an array with specific values and with a given size, then simply put the array elements using JavaScript Array.from() function. This can be useful when you are trying to hold certain values in your array.

Example: In this example, we will pass the elements as arguments, each character will be an array element

Javascript




<script>
    var arr = Array.from('GEEKSFORGEEKS');
    console.log(arr.length);
    console.log(arr)
</script>


Output:

13
["G", "E", "E", "K", "S", "F", "O", "R", "G", "E", "E", "K", "S"]

Method 6: In this method, we will use JavaScript Array.from() function and the repeat() function to create an array with a given size by repeating that number of times.

Example: In this example, we will pass a single element as a parameter and repeat the length we want in our array.

Javascript




<script>
    var arr = Array.from('G'.repeat(5));
    console.log(arr.length);
    console.log(arr);
</script>


Output:

5
["G", "G", "G", "G", "G"]

Method 7: In this method, we will use two features of ES6, one JavaScript Array.from() function and the arrow function to create an array with a given size.

Example:

Javascript




<script>
    var arr = Array.from({length: 5}, (x, i) => i);
    console.log(arr.length);
    console.log(arr)
</script>


Output:

5
[0, 1, 2, 3, 4]

To more about Arrays in JavaScript please go through Arrays in JavaScript


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!