DEV Community

Cover image for .toLocaleString, one of the most underrated JavaScript features

.toLocaleString, one of the most underrated JavaScript features

Siddharth on April 24, 2022

.toLocaleString and friends are some of the most underrated features of JavaScript. I came over them through a few different wanderings through MDN...
Collapse
 
joelbonetr profile image
JoelBonetR πŸ₯‡

Not only in JS, there are bugs in some games due to forgetting about this when dealing with dates in C# in Unity and so on πŸ˜†

Collapse
 
siddharthshyniben profile image
Siddharth

Haha, dates are a pain

Collapse
 
birki profile image
Marcel Birkenkamp

Blain is a Pain

Thread Thread
 
joelbonetr profile image
JoelBonetR πŸ₯‡ • Edited

No Pain no Blain πŸ’ͺπŸΌπŸ˜‚

Collapse
 
eziyadah profile image
Eiyad Z.

I have used it for dates before, but really didn’t realize that I can do all of this ☺️

Great Article! πŸ‘πŸ»πŸ‘πŸ»

Collapse
 
yukimyona profile image
Oluwapelumi Odumosu • Edited

My mind is blown, I've used it a ton of times for date formatting but didn't even know it had some more properties for dates! And that it can also be used for numbers 🀯.
I'm definitely trying this out.
Thanks for this piece.

Collapse
 
busyxiang profile image
busyxiang

Didn't even realise you can do all this, thank you for the sharing

Collapse
 
venkatesanmca008 profile image
venkatesanmca008

good one ..

Collapse
 
ricky11 profile image
Rishi U

awesome, why the .. for some items and not others?

Collapse
 
siddharthshyniben profile image
Siddharth • Edited

In JavaScript, you can't directly access properties on numbers. You can either use 2 dots or braces instead

10.toString() // error
(10).toString() // ok
10..toString() // ok
Enter fullscreen mode Exit fullscreen mode
Collapse
 
triyanox profile image
Mohamed Achaq

I'm using this feature a lot when displaying dates !

Collapse
 
frodolight profile image
Frodo

Thank you.

Collapse
 
thomasbnt profile image
Thomas Bnt β˜•

Good post and useful feature πŸ‘Œ

Collapse
 
siddharthshyniben profile image
Siddharth

Thanks!

Collapse
 
yashdesai profile image
Yash Desai

Thanks for sharing πŸ‘

Collapse
 
amircahyadi profile image
Amir-cahyadi

Nice πŸ‘

Collapse
 
mat3uszkek profile image
Mateusz SkrobiΕ›

I didn't know about that - WOW! this is awesome :) thank u very much for this article!

Collapse
 
augustoapg profile image
Augusto Peres

MIND BLOWN! I’ll use it for sure!! Thanks!!

Collapse
 
arunagnihotri profile image
Arun Agnihotri

I used it for dates only but very basic only. Didn't know all this was possible.
Thanks Siddharth!!

Collapse
 
elainedelgado profile image
Elaine Delgado

Thanks, that helped a lot!!
Goodbye libraries for number formatting! =D

Collapse
 
onedamianocoder profile image
onedamianocoder

clear explanation ! :)

Collapse
 
jofwitsolution profile image
Faleye Oluwafemi Joseph

Exactly what I need at the moment. Thank you so much.

Collapse
 
siddharthshyniben profile image
Siddharth

Was that pun intended 🀣

Collapse
 
lioness100 profile image
Lioness100

What are the differences between Number#toString() and Intl.NumberFormat?

Collapse
 
siddharthshyniben profile image
Siddharth

.toString doesn't provide the features of Intl. It only supports stringifying to a base.

Collapse
 
lioness100 profile image
Lioness100 • Edited

Sorry, I meant .toLocaleString()

Thread Thread
 
siddharthshyniben profile image
Siddharth

The features are the same, except Intl is faster when you need to run the stringification multiple times

Thread Thread
 
lioness100 profile image
Lioness100

Ah, thanks!