Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

444
Views
¿Cuál es la diferencia entre usar un bucle for con let y usar un var?

Estoy desarrollando un sistema pequeño, pero noté que hay una diferencia en usar un ciclo para var y let, normalmente siempre uso let de todos modos, pero decidí hacer algunas comprobaciones con var y eso me dejó alucinado.

Ejemplo:
Esto funciona:

 var inp = document.getElementsByTagName('input'); var focus = 0; var inpLen = inp.length - 1; inp[focus].focus(); for (let i = 0; i < inp.length; i++) { const e = inp[i]; e.onkeyup = function () { if (e.value.length == e.max) { var leap = Math.min(i + 1, inpLen); inp[leap].focus(); } } }
 <input type="number" max="2"> <input type="number" max="3"> <input type="number" max="2"> <input type="number" max="3">

Y no funciona:

 var inp = document.getElementsByTagName('input'); var focus = 0; var inpLen = inp.length - 1; inp[focus].focus(); for (var i = 0; i < inp.length; i++) { var e = inp[i]; e.onkeyup = function () { if (e.value.length == e.max) { var leap = Math.min(i + 1, inpLen); inp[leap].focus(); } } }
 <input type="number" max="2"> <input type="number" max="3"> <input type="number" max="2"> <input type="number" max="3">

Mi pregunta es:
Por qué sucede esto y cómo puedo configurar mi ciclo con var para que funcione correctamente

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!