Avatar ·

How to format a number with spaces or commas in JavaScript for thousands separation?

Hello everyone! I'm writing an online store in JavaScript and encountered this problem: when displaying product prices, numbers appear in an unreadable format. For example, the product price 1500000 is displayed as "1500000", but I would like to see "1 500 000" or "1,500,000". \n\nI tried using toLocaleString(), but for some reason it doesn't work correctly for all numbers. Here's my code:\n\n```javascript\nlet price = 1500000;\nconsole.log(price.toLocaleString()); // Outputs "1500000" (without separators)\n```\n\nI have Russian locale in my browser. I also tried passing parameters:\n\n```javascript\nconsole.log(price.toLocaleString('ru-RU')); // Still "1500000"\n```\n\nMaybe I'm doing something wrong? Or is there another way to add thousands separators? I need a solution that works in all modern