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:01:32
The Rubin Report
4 hours agoTrump Addresses Rumors of Elon Musk & Rubio Clashing in Cabinet Meeting
64.7K32 -
2:09:05
Steven Crowder
6 hours agoTrudeau Out, Carney In: Who Is Canada’s Anti-Trump Puppet?
453K249 -
1:04:31
Timcast
5 hours agoDemocrat Machine IMPLODING, Biden AUTOPEN Scandal & ActBlue's COLLPASE Signal END Of Deep State
115K133 -
1:49:52
Benny Johnson
3 hours agoTrump Orders BLM Plaza DISMANTLED in DC, DHS Launches DEPORTATION App | Secret Service KILL Gunman!?
126K75 -
1:02:07
Kyle Fortch
5 hours ago $0.40 earnedOllie Joseph: 100,000,000 Streams Organically & The Creation Of "Mountain Pop" | THE ONE SHEET S1E7
15.8K -
16:47
T-SPLY
7 hours agoDemocrats Keep Pushing For A Government Shutdown In A Historically Dumb Move
12.4K9 -
2:12:48
Matt Kohrs
16 hours agoStock Market Crash News & Predictions || The MK Show
66.4K9 -
2:01:15
LFA TV
1 day agoKASH AFRAID FOR HIS LIFE! | LIVE FROM AMERICA 3.10.25 11AM
50.8K17 -
1:15:48
The Big Migâ„¢
15 hours agoAnother Trump Assassination Attempt ?
24K6 -
DVR
Bannons War Room
20 days agoWarRoom Live
5.08M1.09K