How does Length work outside and inside For in Javascript?

Asked

Viewed 592 times

1

I have a constant doubt about the Length and the noose for to measure an array or a string.

Doubt 1:

When we use that code:

const numero ="teste";
const medir = numero.length;
console.log(medir)

It returns the value 5. But if we use as an array:

const numero = ["Saab", "Volvo", "BMW"];
const medir = numero.length;
console.log(medir)

It returns 3. Why ?

Doubt 2:

How does it work inside For? I have the code:

   const separador = " - ";

function filtro(funcao, numeros2) {

let stringNova = '';

for (let i = 0; i < numeros2.length; i++) {

    stringNova += numeros2[i] + (funcao)
}
return stringNova
}

console.log(filtro(separador, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));

    for (let i = 0; i < numero.length; i++)

How exactly does this piece of code work? People explain it to me but explain it to me in a technical way and I’m new and it’s not so clear to me.

2 answers

3


Doubt #1:

const numero ="teste";
const medir = numero.length;
console.log(medir)

Its number variable is of the text type (string) the property length returns the number of characters.

const numero = ["Saab", "Volvo", "BMW"];
const medir = numero.length;
console.log(medir)

Its variable is a vector (array) the property length returns the amount of elements.

Doubt #2

Inside the for works equal of the two ways I quoted above, the value of the length will be based on the type of the variable.

The first code returns the amount of elements since you are passing an array as parameter.

const separador = " - ";

function filtro(funcao, numeros2) {

  let stringNova = '';

  for (let i = 0; i < numeros2.length; i++) {
    stringNova += numeros2[i] + (funcao)
  }
  return stringNova
}

console.log(filtro(separador, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));

And that last chunk of code based on that variable numero is the same as the code at the beginning of your question, returns the amount of letters, then will run the looping 5 times.

for (let i = 0; i < numero.length; i++)
  • So this code means that: i(0) is less than numero.length(5 ); will increase until i is equal to numero ?

  • That while i is a minor.

2

The noose for is usually used to traverse the elements of an array. Imagine a street with houses, starting from House 1, House 2, Box 3 and so on. Similarly it is an array, an object that contains elements organized such as a street with houses, except that in the array the address begins with Box 0, House 1 etc., i.e., in an array, the first address is always 0 (called index or index).

So an array with 3 elements, for example, the maximum index it can have is 2, because it starts from 0, 1, 2 (3 items).

var array = ['Maria','Joao','Jose']; // Total de 3 itens
                ↑       ↑      ↑
                0       1      2     // índices

Therefore in the for the variable starts with 0 and goes to the total of items - 1 (3 - 1 = 2), which is the maximum index in the array:

for (let i = 0; i < numeros2.length; i++) {
                          ↑
                enquanto "i" for menor que
              a quantidade de itens na array

Since i is equal to the number of items in the array, the condition in the for (numeros2.length) will no longer be satisfied and the bond is closed.

Browser other questions tagged

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