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
-
LIVE
JuicyJohns
3 hours ago $1.86 earned🟢#1 REBIRTH PLAYER 10.2+ KD🟢$500 GIVEAWAY
112 watching -
LIVE
Caleb Hammer
3 hours agoShe Came Here To Cancel Me | Financial Audit
145 watching -
LIVE
MYLUNCHBREAK CHANNEL PAGE
2 hours agoExposing It All
558 watching -
LIVE
The Big Migâ„¢
5 hours agoA World Without Cancer w/ Expert John A. Richardson Jr.
4,638 watching -
4:17:18
The Bubba Army
1 day agoTrump Pardoning Diddy? - Bubba the Love Sponge® Show | 7/30/25
54.7K12 -
1:19:08
Dear America
3 hours agoGhislaine Maxwell WANTS TO TESTIFY + New Audio Reveals Trump’s Relationship With Epstein!!
123K102 -
LIVE
Badlands Media
10 hours agoBadlands Daily: July 30, 2025
3,987 watching -
2:01:02
Matt Kohrs
11 hours agoHappy Fed Decision Day!!! || Live Trading
25.8K1 -
59:26
Part Of The Problem
13 hours agoDave Smith | War Without Propaganda | Part Of The Problem 1291
14.5K6 -
LIVE
Wendy Bell Radio
8 hours ago2 Minutes And 17 Seconds
6,960 watching