DEV Community

Mehul Lakhanpal
Mehul Lakhanpal

Posted on • Originally published at


Pretty format number using '.toLocaleString()'

// Error
console.log(123456.toLocaleString()); // wont work

// Method 1: Store it in a variable 
const n = 123456;
console.log(n.toLocaleString()); // 123,456

// Method 2: Use Number() to convert it into type Number
console.log(Number(12345).toLocaleString()); // 12,345
Enter fullscreen mode Exit fullscreen mode

Thanks for reading πŸ’™

Follow for more.

Instagram ● Twitter ● Facebook

Micro-Learning ● Web Development ● Javascript ● MERN stack


File Ops - A VS Code extension to easily tag/alias files & quick switch between files

Top comments (0)

11 Tips That Make You a Better Typescript Programmer


1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields


Read the whole post now!