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

84
Vistas
Concatenating a string in a forEach loop js

I’m trying some tester code I turn a string into UpperCase Then split it to make it into an array and then loop through it using ES6 forEach loop However when I try to concatenate ‘hello’ to the subject of the loop It returns undefined

const string = 'abcd'
console.log(string.toUpperCase().split('').forEach(element => element += 'hello'))

And when I add join(‘’) to it it returns this

undefined is not an object (evaluating string.toLowerCase().split('').forEach(element => element +=('hello')).join')
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You need to use map function to make any changes to array elements

const string = 'abcd';

console.log(
  string
    .toUpperCase()
    .split('')
    .map(char =>
      char += ' hello'
    ).join(', ')
);

about 4 years ago · Juan Pablo Isaza Denunciar

0

map is the proper way to do it, but if you need to use forEach, use it like that:

const string = 'abcd'
var newstr = '';
string.toUpperCase().split('').forEach(element => newstr += element + 'hello');
console.log(newstr);

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