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

83
Views
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 answers
Answer question

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 Report

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 Report
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!