1
var tags = new Array();
var tags[4]= new Array();
tags[4].push("Hello");

Somehow this doesnt work, console Says on line two that there's an unexpected Token... Can you help me somehow? Its an Array inside an Array. I simplified the code, because the rest is right.

Thx

4
  • possible duplicate of push() a two-dimensional array with JavaScript Commented Feb 12, 2014 at 13:51
  • @Hast - I really don't see how that is a duplicate... Commented Feb 12, 2014 at 13:52
  • @Lix It mentions two-dimensional arrays and pushing, it must be a duplicate, don't you see? :p Commented Feb 12, 2014 at 13:58
  • @NiettheDarkAbsol - gosh I hope you were being sarcastic :P Commented Feb 12, 2014 at 13:59

6 Answers 6

12

var tags[4] is incorrect. Just tags[4] is needed.

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

1 Comment

@sunfingerde - what error message do you get? This is all that's wrong with your code.
6

It's a simple mistake to make. Just remove var from line 2...

var tags = new Array();
tags[4]= new Array();
tags[4].push("Hello");

tags[4] is already available by declaring tags on line 1.

Comments

3

Remove the var before tags[4]. tags is the variable, tags[4] is a property of the object referenced by that variable, not another variable.

var tags = new Array();
tags[4]= new Array();
tags[4].push("Hello");

Comments

3
var tags[4] // is incorrect.
// use this
tags[4]= new Array();
tags[4].push("Hello");

var keyword creates a variable so old value is lost .

Comments

2

The array tags has already been initialized, so you don't need var on the second line. Remove it and the code works as expected.

Comments

0

Remove var before tag[4]

Try this instead.

var tags = new Array();
tags[4]= new Array();
tags[4].push("Hello");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.