InterviewHubPRO
Section Overview
Q293

Question 293 of 521

Part 5: ES6+ Modern Syntax, Classes, Modules & Destructuring (254 - 328)

56%
JavaScript Internals (521 Qs)•Part 5: ES6+ Modern Syntax, Classes, Modules & Destructuring (254 - 328)

How do you print numbers with commas as thousand separators

You can use the Number.prototype.toLocaleString() method which returns a string with a language-sensitive representation such as thousand separator, currency etc. of this number.

javascript
  function convertToThousandFormat(x) {
    return x.toLocaleString(); // 12,345.679
  }

  console.log(convertToThousandFormat(12345.6789));