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

130
Views
Javascript Replace the first character

How do I replace the first character of a string with another character? I am casting the number as string.

Example: String 12341 New string: 92341

I tried this script but cant figure a way to only replace the first character.

var oldStr =12341;
var newStr = oldStr.replace(1, 9);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. oldStr is not a string, it's a number.

  2. replace has to replace characters, not numbers.

  3. So coerce the number to a string, and then do the replacement.

const oldStr = 12341;
const newStr = oldStr.toString().replace('1', '9');
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!