In JavaScript, which of the following shows valid ways to create an array of values?

Difficulty: Easy

Correct Answer: Using square bracket literals like [1, 2, 3] or the Array constructor like new Array(1, 2, 3)

Explanation:


Introduction / Context:
Arrays are one of the fundamental data structures in JavaScript, widely used to store ordered lists of values such as numbers, strings, or objects. Interviewers often ask how to create arrays, because it tests your familiarity with both modern best practices and older syntax like the Array constructor. Understanding array creation also helps you read and maintain existing code in real projects.


Given Data / Assumptions:

  • We are using standard JavaScript in browsers or Node.js.
  • We want to create an array that can hold multiple values in an ordered list.
  • The question asks which option describes valid and commonly used ways to create arrays.


Concept / Approach:
There are two main ways to create arrays in JavaScript. The recommended approach is to use the array literal syntax, which uses square brackets, for example [1, 2, 3]. The older approach is to use the Array constructor, such as new Array(1, 2, 3), although this can sometimes be confusing when passing a single numeric argument because it creates an array of that length. Other ideas, such as listing multiple values in a single variable declaration or relying on automatic conversion from objects, do not create true arrays and are considered incorrect in this context.


Step-by-Step Solution:
Step 1: The literal syntax uses square brackets and comma separated values, for example var numbers = [1, 2, 3]; This is the cleanest and most common method. Step 2: The Array constructor can also create arrays using new Array(1, 2, 3). This constructs an array with the elements 1, 2, and 3 in order. Step 3: Declaring var a = 1, 2, 3; simply declares one variable a with the last value, 3, and does not create an array. Step 4: Creating a plain object with numeric property names like {0: "a", 1: "b"} does not automatically convert it into an Array; arrays are special objects with their own prototype and length handling. Step 5: The String constructor always returns string objects, not arrays, so it cannot be used to create arrays of arbitrary values.


Verification / Alternative check:
In a JavaScript console, if you type Array.isArray([1, 2, 3]), it returns true, confirming that the literal syntax creates an array. Similarly, Array.isArray(new Array(1, 2, 3)) returns true. In contrast, Array.isArray({0: "a", 1: "b"}) returns false, and var a = 1, 2, 3; leaves a as a single number, not as an array. These tests confirm that the correct way to create arrays is through literal syntax or the Array constructor.


Why Other Options Are Wrong:
Option B is wrong because the comma operator in JavaScript evaluates each expression and returns the last one, so var a = 1, 2, 3 results in a being equal to 3, not an array. Option C is incorrect because plain objects do not magically become arrays; arrays have special behaviour such as automatic length updates and inherited methods like push() and pop(). Option D is wrong because the String constructor creates string objects, and no overload of String turns a list of values into an array.


Common Pitfalls:
A very common pitfall is using new Array(10) expecting an array with one element 10, but instead getting an empty array with length 10. Another issue is confusing array like objects such as arguments or NodeList with true arrays, which can cause method calls like map() to fail until you convert them. Using the literal syntax [ ] avoids many of these pitfalls and is recommended for most modern JavaScript code.


Final Answer:
JavaScript arrays are commonly created using square bracket literals like [1, 2, 3] or with the Array constructor like new Array(1, 2, 3).

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion