How can I create an array in Javascript

Asked

Viewed 432 times

0

Hello I recently started using this platform to try to clarify some doubts and I’m having some difficulties in creating arrays to store data through Javascript.

I think the mistake is something simple yet I can’t determine what it is.

 carros = ["Fiat", "Ferrari", "Toyota"];

I appreciate all the help already provided.

  • 10

    If the problem was creating an array, congratulations ,vc have already created. Just give a console.log(cars) that will see the array. You can give a length cars. to see the size of the array among many other things that can be done.

  • 2

    What exactly is your question? You put the javascript code in the snippet as html, and so the code appears written on the screen, because normally nothing would appear. Creating an array does not imply showing anything anywhere.

  • 1

    This array you have placed can be accessed like this: carros[0] (will return the first index of the array), and so on. You access the array elements using square brackets, with the first index being 0 (zero).

  • 1

    I couldn’t explain it very well but the user @Davidmv has already helped me in this case. But I appreciate the information given for future questions on this platform :)

2 answers

0


Hello If you want to store in different variables you can use this function:

var carro1 = "Fiat";
var carro2 = "Ferrari";
var carro3 = "Toyota";

if you want to put everything inside the same variable you can do this way:

var carros = ["Fiat", "Ferrari", "Toyota"];

or this:

var carros = [
        "Fiat", 
        "Ferrari", 
        "Toyota"
        ];

You can use to help you with programming this site: W3schools

0

You just forgot to var at first, Example.:

var carros = ["Parati", "Ferrari", "BMW"]

You can also use Let

let carros = ["Parati", "Ferrari", "BMW"]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.