Premium Only Content
Compact representation of a large number in JavaScript ( #shorts )
Hi Friends,
Compact representation of a large number in JavaScript
To generate a compact representation of a large number in JavaScript, you can use the following function
function getCompactNumber(numbr) {
const units = ['', 'K', 'M', 'B', 'T'];
let unitIndex = 0;
while (numbr ≥ 1000 && unitIndex < units.length - 1) {
numbr /= 1000;
unitIndex++;
}
return numbr.toFixed(1).replace(/\.0$/, '') + units[unitIndex];
}
getCompactNumber(1000); // "1K"
getCompactNumber(1200); // "1.2K"
getCompactNumber(1200000); // "1.2M"
getCompactNumber(1200000000); // "1.2B"
getCompactNumber function takes a number as an argument and returns a string representing the number in a compact format. The function uses an array of units to check which unit to use for the compact representation, and then divides the number by 1000 until it is less than 1000 or the largest unit has been used. The number which will be returned as result, is then formatted to one decimal place and any trailing ".0" is removed.
#javascript #compact #number #compactnumber #large #largenumber #string #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #famous #coders #development #code #dev #interview #php #trend #answer #youtube #instagram #socialmedia #education #edit #gaming #how #india #knowledge #love #like #motivational #quiz #reels #short #subscribe #share #update #video #ytshorts #youtuber #princecheema_official @princecheema_official
-
1:07:25
Timcast
3 hours agoBomb DETONATED At Harvard, Attacks On Ice Agents SKYROCKET
149K126 -
1:55:31
Steven Crowder
5 hours agoTucker Carlson & MAGA: Everyone is Missing the Point
377K299 -
1:11:22
The Rubin Report
4 hours agoWatch Joe Rogan’s Face as Elon Musk Exposes How Dems Are Cheating in Plain Sight
62.1K83 -
1:01:07
VINCE
6 hours agoThe Walls Are Closing In On The Deep State | Episode 160 - 11/03/25
245K152 -
LIVE
LFA TV
21 hours agoLIVE & BREAKING NEWS! | MONDAY 11/3/25
1,867 watching -
1:31:18
Graham Allen
6 hours agoErika Fights Back: Vows To EXPOSE TRUTH & DEMANDS Trial Goes Public!! Left Says Her Grief Is FAKE!
159K86 -
2:08:47
Badlands Media
11 hours agoBadlands Daily: November 3, 2025 – Tariff Wars, SNAP Panic & Brennan Gets Confronted
78.9K21 -
2:59:32
Wendy Bell Radio
10 hours agoThings Will Get Worse Before They Get Better
101K117 -
1:18:28
The Big Migâ„¢
6 hours agoICE Will Use Private Bounty Hunters, LFG
42.4K13 -
1:08:17
Chad Prather
13 hours agoHow to Get Along With People You Don’t Even Like (Most of the Time)
129K36