What does [] mean after a variable?

Asked

Viewed 375 times

-5

I wonder what that is, when a variable gets that []:

let teste;
    
teste[id]
  • 1

    An indexer...

  • Search by indexing on google

  • Where did you see that? There’s no code missing there?

  • 1

    About the let, is answered here. About the [], read the documentation about arrays

3 answers

2


Without a larger context regarding the code, the most we can say is:

The let is one of the ways to declare a variable, and influences the scope of it - all of this has been explained in detail here. So let teste; is only declaring a variable called teste (and its scope will follow the rules explained in link already quoted, so it will depend a lot on where it was declared - and without more context, that’s all you can say).

Another answer said that "this is the declaration of the test variable of type any", but in fact the guy any exists only in Typescript. In Javascript any variable can be of any type, and you do not declare the type in the code (different from other languages in which you are required to inform the type - read more on the subject here, here and here). And as indicated above, let only serves to define the scope, not the type.


About teste[id], this is a syntax to access an element or property of teste, depending on the case. A same answer already cited said that "this makes the test variable in an array", which is not true. The variable only "becomes" an array if we assign a value to it, provided, of course, that that value is an array. For example:

let teste = [1, 2, 3]; // array com os números 1, 2 e 3

Or

let teste = Array(10); // array com 10 posições

Finally, read the documentation about arrays to better understand what it is and the various ways to create a.

From there, we can access the elements of this array, using the brackets. Ex:

let teste = ['a', 'b', 'c'];
console.log(teste[0]); // 'a'
console.log(teste[1]); // 'b'
console.log(teste[2]); // 'c'

The first element is at index 0, the second at index 1 and so on. But we can also use a variable as an index instead of a fixed value:

let teste = ['a', 'b', 'c'];
let id = 2;
console.log(teste[id]); // 'c'

That is to say, teste[id] is accessing the element whose position is the value indicated by the variable id. This does not "make" the variable teste in an array (because it was already an array before).


But this is not restricted to arrays. Any property of any object can be accessed thus:

let teste = { 'nome': 'Fulano', 'idade': 20 };
// acessando a propriedade "nome"
let id = 'nome';
console.log(teste[id]); // 'Fulano'

// mas também poderia ser assim:
console.log(teste['nome']); // 'Fulano'
console.log(teste.nome); // 'Fulano'

Remember that arrays can also have properties:

let teste = [1, 2, 3];
// criando a propriedade "nome"
teste['nome'] = 'sou um array';

// acessando a propriedade "nome"
let id = 'nome';
console.log(teste[id]); // 'sou um array'

// mas também poderia ser assim:
console.log(teste['nome']); // 'sou um array'
console.log(teste.nome); // 'sou um array'

Finally, about teste[id], is "only" this, but you can delve into the assumption by reading here, here, here and here.

0

When talking about array, the indexer is the position of the element within the array, since if this model is used in objects, we will be referencing a key of the object.

Example:

let arr = ['posicao1', 'posicao2', 'posicao3'];

let obj = {
  nome: 'Teste',
  idade: 25
}

console.log(arr[2]); // retorno será 'posicao3', pois é o elemento 2 do array, lembrando que array começa em 0,1,2...

console.log(obj['nome']); // Em objeto, o indexador é a key do objeto, neste caso o retorno será o valor da propriedade nome, ou seja 'Teste';

-3

let teste; <- this is the declaration of the variable test of type any, or is a variable that can be anything.

teste[id] <- this makes the test variable in an array and id is another variable being used as an indexer of a position in the array.

  • as an indexer?

  • 2

    "turns the test variable into an array"?

  • 2

    "this makes the test variable in an array" - no, what makes it an array would be something like teste = [ 1, 2 ]. Already teste[id] only serves to access a specific element, but the variable was already an array before that (it has already "become" an array before of that declaration, and because of her). And the type any only exists in Typescript. In Javascript any variable can be of any type, and of qq form the let only influences the scope of the variable, and not in the type. It is worth remembering that teste could be an object (not just an array) and id would contain the name of the property

Browser other questions tagged

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