An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
January 16, 2021 10:08 pm GMT
Original Link: https://dev.to/suprabhasupi/numeric-separators-in-javascript-3jec
Numeric Separators in JavaScript
Numeric Separators give us the ability to separate thousands with an underscore (_) in numeric literals.
How its useful
It makes our code more informative and readable.
let series = 10000;
Enter fullscreen mode Exit fullscreen mode
Numeric Separators in javascript, enables underscore as a separator in numeric literals to improve readability.
Example:
let series = 1_00_00;
Enter fullscreen mode Exit fullscreen mode
You can also use this for binary, octal, and hex numbers.
Binary Number
let series1 = 0b1010_0101_1001;console.log(series1); // 2649
Enter fullscreen mode Exit fullscreen mode
Octal Number:
let series2 = 0o2_3_5_7;console.log(series2); // 1263
Enter fullscreen mode Exit fullscreen mode
Hex Number:
let series3 = 0xA_B_C_D_E;console.log(series3); // 703710
Enter fullscreen mode Exit fullscreen mode
Few Limitation
Below limitation snippet will throw SyntaxError
More than one underscore in a row is not allowed
let series1 = 100__000;
Can not be used after leading 0
let series2 = 0_1;
Not allowed at the end of numeric literals
let series3= 100_;
Browser Support
This feature has pretty good support in recent versions of browsers.
Check this out
Reference
Original Link: https://dev.to/suprabhasupi/numeric-separators-in-javascript-3jec
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To