What is the correct way to write a JavaScript array?

  1. var txt = new Array(1:"app",2:"cop,3:"top")

  2. var txt = new Array("app","cop","top")

  3. var txt = new Array:1=("tim")2=("cop")3=("top")

  4. var txt = new Array="app","cop","top"


Correct Option: B

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) var txt = new Array(1:"app",2:"cop,3:"top") - This option is incorrect because when creating a JavaScript array using the new Array() syntax, the correct way to define the elements is by separating them with commas inside square brackets, not colons. The correct syntax would be var txt = new Array("app", "cop", "top").

Option B) var txt = new Array("app","cop","top") - This option is correct because it follows the correct syntax for creating a JavaScript array. The elements are enclosed in square brackets, and they are separated by commas.

Option C) var txt = new Array:1=("tim")2=("cop")3=("top") - This option is incorrect because it does not follow the correct syntax for creating a JavaScript array. The colons after the Array constructor and the equal signs after each index value are not valid syntax.

Option D) var txt = new Array="app","cop","top" - This option is incorrect because it does not follow the correct syntax for creating a JavaScript array. The equal sign after the Array constructor is not valid syntax.

The correct answer is B. This option is correct because it follows the correct syntax for creating a JavaScript array, with the elements enclosed in square brackets and separated by commas.

Find more quizzes: