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

175
Views
Why is .splice(1, 0, '.') returning an empty array? (Javascript)

Here is my code:

let num = 0.0134;

console.log(num.toString().split('').splice(3).splice(1, 0, '.'))

The console.log returns an empty array.

I want it to return '1.34'. What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you see the reference for Array.prototype.splice on mdn:

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place

Return Value: An array containing the deleted elements.

At the end of your code, when you do .splice(1, 0, '.'), it deletes 0 elements so it will return an empty array.

I'm assuming you want to get the previous array with the '.' element inserted in the first position. For that you'll want to do:

const arr = num.toString().split('').splice(3);
arr.splice(1, 0, '.');
console.log(arr)

And If you want to join it to a string just use arr.join('')


Anyway, for your use case, you could just use the * multiplication operator rather than converting to a string array and attempting to manipulate that.

You can simply do:

let num = 0.0134;
console.log(num * 100);
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!