JavaScript

iliya caspi
iliya caspi
3,403 Points

what is the difference between those two declarations within for loop

let token = new Token(i, this);

tokens.push(token);

or

tokens[i]=new Token(i, this);

Adam VanSlyke
Adam VanSlyke
15,979 Points

Hi Iliya,

let token = new Token(i, this); tokens.push(token); The above code will create a new token object (line 1) and 'push' it onto the end of the existing 'tokens' array.

tokens[i]=new Token(i, this); This code will take the current value of 'i' and place the new token object in that index of the array.

For example if i = 3, it will place the new token object in the 4th place in the array.

Hope this helps.