Display value of an array index in an alert [closed]

-4

Hello

How do I display a value from an array index in an alert in javascript?

a[i] = "Carro";
alert(a[i]);

Thank you

    
asked by anonymous 10.10.2017 / 14:57

3 answers

0

Apparently your idea of how to display the value is not wrong, just correct the syntax.

var a = [];
a[0] = 'Carro';
alert(a[0]); 

In case the array is inside a loop:

    var a = [];
    a[0] = 'Carro';
    
    for (i = 0; i < 1; i++) {
      alert(a[i]);
    }   
   
    
10.10.2017 / 15:02
0

What you are trying to do is to use the variable i to declare the index, but it is not within for

let arr = ["Carro", "Avião", "Moto"]

// Aqui você faz passando o indice manualmente
alert(arr[0])

// Nesse caso você usa o I, ele irá percorrer todos os indices
for(let i = 0; i < arr.length; i++){
  console.log(arr[i])
}
    
10.10.2017 / 15:04
0

Question not very clear.

First of all, you do not need to take this turn

var a = [];
a[0] = 'Carro';
alert(a[0]);

To display this alert, just alert("Carro");

The way the question is asked (and subtending within a loop) would have this return:

var a = ["Saab", "Volvo", "BMW"];

    for (i = 0; i < a.length; i++) {
      //a cada iteração o valor do elemento é alterado para Carro
      a[i] = "Carro";
      console.log(a[i]);
    }

Most likely you're looking for the below

Inside a loop to display array elements

var a = ["Saab", "Volvo", "BMW"];

    for (i = 0; i < a.length; i++) {
      console.log(a[i]);
    }
    
10.10.2017 / 15:25