What is the correct way to write a JavaScript array?

  1. var txt = new Array(1:"tim",2:"shaq",3:"kobe")

  2. var txt = new Array="tim","shaq","kobe"

  3. var txt = new Array("tim","shaq","kobe")


Correct Option: C

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:"tim",2:"shaq",3:"kobe") This option is incorrect because when using the new Array() syntax, you should not specify the indices explicitly. Instead, you can initialize the array directly with the values.

Option B) var txt = new Array="tim","shaq","kobe" This option is incorrect because the syntax is incorrect. When creating a new array, you should use parentheses to enclose the values, not an equal sign.

Option C) var txt = new Array("tim","shaq","kobe") This option is correct because it uses the correct syntax for creating a JavaScript array. The values are enclosed in parentheses, and each value is separated by a comma.

The correct answer is C. This option is correct because it follows the correct syntax for creating a JavaScript array using the new Array() syntax.

Find more quizzes: