Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
Not quite understand what does the i stand for in forEach function in my code

I am using this method to find the frequency of words, however i am a bit confused with the whole code.

Here is the code:

function check(){
 var word = document.querySelector('textarea').value.split(" ");
   frequency ={};
   word.forEach(function(i){
      console.log(i)
      if(!frequency[i]){
         frequency[i] = 0;
      }
      frequency[i]+=1
   })
console.log(frequency)
}
<textarea></textarea>
<button onclick = 'check()'>check</button>

I just wonder what does the i stand for and what does the frequency[i].

Could someone explain to me a little bit because I think this code is not quite friendly for me, the beginner.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

foreach iterates over array, and as paramter has function into which is passed as parameter actual element of array. So i in this function is actual element of 'word' array.

for object frequency is frequency[i] the i'th element. At start, this object is empty, so frequency[i] will be undefined, but in foreach loop you filling this object with some values so in next iterations there may by some values

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda