Which is the correct way to write a JavaScript array?
A. var txt = new Array(1:"tim",2:"kim",3:"jim")
B. var txt = new Array:1=("tim")2=("kim")3=("jim")
C. var txt = new Array("tim","kim","jim")
D. var txt = new Array="tim","kim","jim"
Answers
JAVASCRIPT ARRAY
Explanation:
var txt = new Array("tim","kim","jim")
is the correct answer according to javascript syntax
the entire array is stored in variable valled txt
so
var txt is the variable decleration suntax
new Array() is the initiation of new array by creating new object
coming to option 2
var txt = new Array:1=("tim")2=("kim")3=("jim")
Array wont take the parameters like this in the option 2
1=("tim")2=("kim")3=("jim")
Generally this type of syntax is used when passing as arguments to the functions
Coming to option1
new Array(1:"tim",2:"kim",3:"jim")
the Array wont accept key and value pairs as input and it can not store it
In other option
var txt = new Array="tim","kim","jim"
the array decleration is not grouped into a parenthesis
so it won't take entire array it just take the first element after “=” sign