By Александр Ковтун

2018-06-13 14:11:13 8 Comments

I have a number of unknown length and i need to format it by adding comma after every 3rd number from the right (so 12345678 becomes 12,345,678). As of now i'm using this chaos to do it

value.toString().split('').reverse().join('').match(/.{1,3}/g).reverse().map( (num) => num.split('').reverse().join('') ).join()

Is there any cleaner way to do it with native functionality?


@VicJordan 2018-06-13 14:14:07

Here is the simplest way to do this.

Check below:

let number = 12345678;


Reference -> here

Related Questions

Sponsored Content

25 Answered Questions

37 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

64 Answered Questions

[SOLVED] What is the most efficient way to deep clone an object in JavaScript?

53 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

88 Answered Questions

[SOLVED] How do JavaScript closures work?

69 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 5217343 View
  • 6521 Score
  • 69 Answer
  • Tags:   javascript arrays

28 Answered Questions

[SOLVED] For-each over an array in JavaScript?

48 Answered Questions

[SOLVED] Validate decimal numbers in JavaScript - IsNumeric()

48 Answered Questions

Sponsored Content