-7

I need some help from you guys!

This is my JS code:

var myArray[2] = "Hi";

It says: "SyntaxError: missing ; before statement".

I dont't see where the hell I forgot an ";" in the easiest code ever.

3

4 Answers 4

3

Valid array in JS:

var myArray = new Array(3); // 3 - count of elements
myArray[2] = 'Hi';  // third element has a value "Hi"

Read more about arrays here

Sign up to request clarification or add additional context in comments.

Comments

2

You have to declare the array first

var myArray = Array();
myArray[2] = 'hi';
console.log(myArray )

Comments

0

Its either a variable declaration:

 var myArray = "Hi";

Or you set a property:

myArray[2] = "Hi";

2 Comments

Best answer in context, but... why are all answers so simplistic? Then they don't need to answer
@hand1cloud i answer to help the OP. I just needed 10 seconds to do so. And another 5 to flag as dupe...
0

You have to declare your aray before using it. Try this:

var myArray = [];
myArray[2] = "Hi";

But then you have a gap at the beginning! Beware of that!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.