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
- 	
				
							3:12:05
								Tundra Tactical
6 hours ago $10.28 earnedProfessional Gun Nerd Plays Battlefield 6
42.3K5 - 	
				
							1:01:12
								Donald Trump Jr.
8 hours agoThe China Matrix with Journalist Lee Smith | TRIGGERED Ep.288
136K86 - 	
				
							11:56:00
								Dr Disrespect
13 hours ago🔴LIVE - DR DISRESPECT - ARC RAIDERS - FULL SEND INTO THE RED
157K18 - 	
				
							LIVE
								JdaDelete
5 hours ago $0.35 earnedFinally playing Eldin Ring | First Playthrough Episode 2
59 watching - 	
				
							1:02:08
								BonginoReport
7 hours agoNicki Minaj Speaks Out Against Christian Persecution - Nightly Scroll w/ Hayley Caronia (Ep.169)
64.6K50 - 	
				
							5:23:36
								HomieQuest
7 hours agoLive Streaming! Pokemon Legends Z-A
3.73K3 - 	
				
							5:33:02
								FusedAegisTV
10 hours agoFUSEDAEGIS PLAYS THE GREATEST JRPG EVER MADE ⌛► CHRONO TRIGGER (1995) Part 3
3.26K2 - 	
				
							2:13:20
								Nerdrotic
6 hours ago $2.97 earnedNerdrotic At Night 531
31.7K4 - 	
				
							1:43:27
								Glenn Greenwald
7 hours agoThe Right's Crusade to Cancel Tucker | SYSTEM UPDATE #542
74.7K99 - 	
				
							2:10:04
								Conductor_Jackson
1 day agoLet's Play Unrailed 2 Solo! 🚂🚂🚂🚂🚂🚂
9.02K1