Which of the following is the correct way to declare an array named myarray with ten elements?

I'm just learning JavaScript and it seems like there are a number of ways to declare arrays.

  1. var myArray = new Array()
  2. var myArray = new Array(3)
  3. var myArray = ["apples", "bananas", "oranges"]
  4. var myArray = [3]

What are their difference, and what are the preferred ways?

According to this website the following two lines are very different:

var badArray = new Array(10); // creates an empty Array that's sized for 10 elements
var goodArray= [10];          // creates an Array with 10 as the first element

As you can see these two lines do two very different things. If you had wanted to add more than one item then badArray would be initialized correctly since Javascript would then be smart enough to know that you were initializing the array instead of stating how many elements you wanted to add.

Is what the authors trying to say is Array(10) creates an array with precisely 10 elements and [10] creates an array of undefined size with the 0th element being 10? Or what does this mean?

Which of the following is the correct way to declare an array named myarray with ten elements?

asked Jul 9, 2012 at 21:00

Which of the following is the correct way to declare an array named myarray with ten elements?

CeleritasCeleritas

14.1k35 gold badges107 silver badges188 bronze badges

4

In your first example, you are making a blank array, same as doing var x = []. The 2nd example makes an array of size 3 (with all elements undefined). The 3rd and 4th examples are the same, they both make arrays with those elements.

Be careful when using new Array().

var x = new Array(10); // array of size 10, all elements undefined
var y = new Array(10, 5); // array of size 2: [10, 5]

The preferred way is using the [] syntax.

var x = []; // array of size 0
var y = [10] // array of size 1: [1]

var z = []; // array of size 0
z[2] = 12;  // z is now size 3: [undefined, undefined, 12]

answered Jul 9, 2012 at 21:06

gen_Ericgen_Eric

218k40 gold badges297 silver badges335 bronze badges

The preferred way is to always use the literal syntax with square brackets; its behaviour is predictable for any number of items, unlike Array's. What's more, Array is not a keyword, and although it is not a realistic situation, someone could easily overwrite it:

function Array() { return []; }

alert(Array(1, 2, 3)); // An empty alert box

However, the larger issue is that of consistency. Someone refactoring code could come across this function:

function fetchValue(n) {
    var arr = new Array(1, 2, 3);

    return arr[n];
}

As it turns out, only fetchValue(0) is ever needed, so the programmer drops the other elements and breaks the code, because it now returns undefined:

var arr = new Array(1);

answered Jul 9, 2012 at 21:02

To declare it:

var myArr = ["apples", "oranges", "bananas"];

To use it:

document.write("In my shopping basket I have " + myArr[0] + ", " + myArr[1] + ", and " + myArr[2]);

answered Apr 21, 2018 at 18:28

ZackZack

911 silver badge1 bronze badge

1

There are a number of ways to create arrays.

The traditional way of declaring and initializing an array looks like this:

var a = new Array(5); // declare an array "a", of size 5
a = [0, 0, 0, 0, 0];  // initialize each of the array's elements to 0

Or...

// declare and initialize an array in a single statement
var a = new Array(0, 0, 0, 0, 0); 

user664833

17.5k18 gold badges89 silver badges133 bronze badges

answered Jul 15, 2013 at 21:46

DeepakDeepak

791 silver badge2 bronze badges

2

If you are creating an array whose main feature is it's length, rather than the value of each index, defining an array as var a=Array(length); is appropriate.

eg-

String.prototype.repeat= function(n){
    n= n || 1;
    return Array(n+1).join(this);
}

answered Jul 9, 2012 at 23:11

kennebeckennebec

101k31 gold badges104 silver badges126 bronze badges

Which of the following is the correct way of declaring an array?

Which of the following correctly declares an array? Explanation: Option A is correct. Int is the data type used,geeks is the name of the array and [20] is the size of the array.

What would be the correct syntax to declare an array with 10 integer values?

Array Initialization in Java int[] intArray = new int[10]; This allocates the memory for an array of size 10 . This size is immutable. Java populates our array with default values depending on the element type - 0 for integers, false for booleans, null for objects, etc.

How can we create an array of 10 integers in C++?

int foo[] = { 10, 20, 30 }; int foo[] { 10, 20, 30 }; Here, the number of the array n is calculated by the compiler by using the formula n= #of initializers/sizeof(int). Static arrays, and those declared directly in a namespace (outside any function), are always initialized.

What is the valid range of index values for an array of size 10?

The following declaration creates an array of 10 elements: double[] values = new double[10]; An index can be any integer ranging from 0 to 9. An array index must be at least zero and less than the size of the array.