skip to Main Content

I’m working out of a text book JavaScript All-In_One. I’m following the directions to write an array. I’m using vscode. This is my array:

const list = new Products['eggs','milk','cheese','garlic','onion','kale','salt','pepper']; 

When I run visual studio I get the error. Products not defined.

I tried declaring the array before assigning it. When I run the code I expect to get the list in the array. When I write Products.length I expect to get the length of the array characters. I tried using the console in the browser and I get the same error.

2

Answers


  1. If the Products constructor accepts an array argument:

    const list = new Products(['eggs','milk','cheese','garlic','onion','kale','salt','pepper']);
    

    The syntax for instantiating a class is:
    new <class_name>(arg1, arg2, ...);

    Login or Signup to reply.
  2. It looks like you were trying to do:

    const Products = new Array('eggs','milk','cheese','garlic','onion','kale','salt','pepper'); 
    console.log(Products.length,Products)
    // or ...
    const Products2=['eggs','milk','cheese','garlic','onion','kale','salt','pepper']; 
    console.log(Products2.length,Products2);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search