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

FFLNVB
  • 147
  • 1
  • 12

6 Answers6

12

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

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
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.

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67
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");
Tibos
  • 27,507
  • 4
  • 50
  • 64
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 .

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
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.

cajunc2
  • 206
  • 1
  • 3
0

Remove var before tag[4]

Try this instead.

var tags = new Array();
tags[4]= new Array();
tags[4].push("Hello");
Navin
  • 594
  • 2
  • 11