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

186
Vistas
how to get the next item from an javascript array?

So, i have built and array of all the month and i am to get the current month from it by using the getDate function but i wanna scroll through it

const month = ["Januray", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

let d = new Date();
let showMonth = month[d.getMonth()]
monthEl.textContent = showMonth

how to i get the next month when i click on a element with onclick="next()" i have read other various solutions but those consists of complex arrays that i am unable to comprehend due to myself being new programming

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

0

You can get the next month by keeping track of the index of the array. Increment the index on a call to next() and pull out the next month from the array using that incremented index month[nextIndex];

The only gotcha is if the index grows beyond the length of the array, which will result in you getting back undefined instead of the month- so we have to check for this. if (nextIndex > month.length - 1) then set the index back to the beginning, 0 and now you have something that will loop through the array.

also you have a small typo in Janurary and Feburary.

const monthEl = document.getElementById("monthEl");
let monthIndex = 0;
const month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

let d = new Date();
monthIndex = d.getMonth();
let showMonth = month[monthIndex]
monthEl.textContent = showMonth;

function next() {
  let nextIndex = ++monthIndex;
  if (nextIndex > month.length - 1) {
    nextIndex = 0;
    monthIndex = 0;
  }

  let nextMonth = month[nextIndex];
  monthEl.textContent = nextMonth;

}
<div id="monthEl"></div>
<button onClick="next()">next</button>

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