Anurag
Anurag

Reputation: 1

javascript array is not working in console

I have arrays in my code like this:

let shoppinglist = ['laptop','ram','screen'];

let anynum = [1,2,nan,true,null,'dog'];

let colour = ['red'];

let shoppingList2 = ['cheese','2 milk'];

let myapp = [0,1,2,3,4,5]; 

but I got this error when running:

Uncaught SyntaxError: Identifier 'colour' has already been declared
myapp
VM192:1 Uncaught ReferenceError: myapp is not defined
    at <anonymous>:1:1
(anonymous) @ VM192:1
shoppinglist.length
VM240:1 Uncaught ReferenceError: shoppinglist is not defined
    at <anonymous>:1:1
(anonymous) @ VM240:1
shoppingList2
VM264:1 Uncaught ReferenceError: shoppingList2 is not defined
    at <anonymous>:1:1
(anonymous) @ VM264:1   

Upvotes: 0

Views: 261

Answers (3)

Nayana Chandran
Nayana Chandran

Reputation: 1483

The value nan in the array anynum, is not just a value. nan is a keyword (built-in) put it as a string like let anynum = [1,2,'nan',true,null,'dog']; other than that array declaration is correct.

enter image description here

Upvotes: 0

Pramod
Pramod

Reputation: 26

You have done a simple mistake there, nan is a keyword in javascript. You can make this as a string otherwise you can make it NaN.

  let shoppinglist = ['laptop','ram','screen'];

  let anynum = [1,2,NaN,true,null,'dog'];

  let colour = ['red'];

  let shoppingList2 = ['cheese','2 milk'];

  let myapp = [0,1,2,3,4,5];

Upvotes: 0

Gui Dadald
Gui Dadald

Reputation: 51

You most likely already declared the variable name 'colour' earlier in your code, so if you want to reassign it, use just:

colour = ['red'];

However, this will not work it you created the variable as 'const'. And nan should be NaN. In relation to the other errors, can you share the beginning of your code?

Upvotes: 2

Related Questions