How to create an array of integers in JavaScript?

In this article, we will learn to create an array of integers in JavaScript. The Array parameter is a list of strings or integers. When you specify a single numeric parameter with the Array constructor, you specify the initial length of the array. The maximum length allowed for an array is 4,294,967,295.

Different Approaches

The following are the two different approaches to creating an array of integers in JavaScript ?

Using Array Literals

One of the simplest ways to create an array of integers in JavaScript is by using array literals, which involve enclosing elements within square brackets [].

Syntax

var arrayName = [element1, element2, element3, ...];

Basic Example






Numbers: 1, 2, 3, 4, 5
Ages: 25, 30, 45, 18
Scores: 85, 92, 78, 95, 88

Array Comparison Example

The following example demonstrates creating two integer arrays and finding common elements:

    
  



Array 1: 50, 60, 65, 90
Array 2: 25, 35, 50, 90

Common elements:
Matched element: 50
Matched element: 90

Using the Array Constructor

JavaScript provides another way to create arrays using the Array constructor. This method allows the creation of an array dynamically by specifying its size or directly assigning values.

Syntax

// Create array with specified length
var arrayName = new Array(length);

// Create array with initial values
var arrayName = new Array(element1, element2, element3, ...);

Creating Array with Initial Values






Grades: 85, 90, 78, 92, 88
Temperatures: 25, 30, 28, 35

Creating Empty Array with Specified Length

    
  



Array length: 4
Array elements: 10, 20, 30, 40

Comparison

Method Syntax Best For Performance
Array Literals [1, 2, 3] Known values Slightly faster
Array Constructor new Array(1, 2, 3) Dynamic creation Slightly slower

Key Points

  • Array literals are more commonly used and preferred for their simplicity
  • Array constructor is useful when you need to create arrays dynamically
  • Both methods create the same type of array object
  • Arrays in JavaScript can hold integers of any size within JavaScript's number limits

Conclusion

JavaScript provides two main methods to create integer arrays: array literals ([]) and the Array constructor. Array literals are preferred for their simplicity and readability, while the constructor is useful for dynamic array creation. Both approaches create fully functional arrays for storing and manipulating integer data.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2026-03-15T23:18:59+05:30

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements