惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
AWS News Blog
AWS News Blog
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
腾讯CDC
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
爱范儿
爱范儿
IT之家
IT之家
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题
L
Lohrmann on Cybersecurity
Scott Helme
Scott Helme
G
GRAHAM CLULEY
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
博客园 - 聂微东
有赞技术团队
有赞技术团队
J
Java Code Geeks
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
V2EX
The Last Watchdog
The Last Watchdog
S
Securelist
博客园 - Franky
罗磊的独立博客
阮一峰的网络日志
阮一峰的网络日志
AI
AI
雷峰网
雷峰网
博客园 - 司徒正美
L
LINUX DO - 热门话题
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LINUX DO - 最新话题
TaoSecurity Blog
TaoSecurity Blog
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
H
Hacker News: Front Page
量子位
Latest news
Latest news

ValeriaVG

Agentic Development Survival Guide Simplicity Police The sunset of Arrogance as a Service How do I delegate when I can do it faster myself? Implementing custom Calendar component Plug & Play Modular Architecture for Scalable and Maintainable Apps ES Modules & Import Maps: Back to the Future Me & React: 5 Years in 15 Minutes 10 Reasons NOT to Use Go for Your Next Project How Build a Web App in 11 Minutes and Fall in Love With Sveltekit Master Git in 7 Minutes Crc 32 Checksum in Wasm and Raw Js Tutorial and Benchmark Master Binary in Five Minutes Five Pro Tips to Master Promises in Js How to Use Custom Files as Modules in Nodejs 5 Ways to Use Redis in Your Next Project
How to Do Magic With Numbers
Valeria Viana Gusmao · 2021-01-24 · via ValeriaVG

JavaScript Number type would have been called double or float64 in a compiled language. Therefore, numbers have some limits:

const maxInt = Number.MAX_SAFE_INTEGER; // 9007199254740991 const minInt = Number.MIN_SAFE_INTEGER; // -9007199254740991 minInt === -maxInt; // true const maxDouble = Number.MAX_VALUE; // 1.7976931348623157e+308 const minDouble = Number.MIN_VALUE; // -1.7976931348623157e+308

See that strange long number in min and max values? That is the first magical way of representing a JavaScript number: using a base and an exponent (a.k.a beN):

const minusTwo = -2; // -2 const one75 = 175e-2; // 1.75

In practice, you specify a base number, then write e and specify where you want to move the dot. If the last number is positive - you'll be adding zeros, if negative - you'll be moving the dot to the left:

const zero = 0; // 0; move dot from 0.0 zero times const zero5 = 5e-1; // 0.5; move dot from 5.0 one time left const fifty = 5e1; // 50; move dot from 5.0 one time right const alsoFifty = 5e1; // 50 const minusFifty = -5e1; //-50; move dot from -5.0 one time right const seventeen5 = 1.75e1; // 17.5; move dot from 1.75 one time right

But, this representation may be a bit hard to read, so you can also use old plain decimals with the underscore as a separator:

const million = 1_000_000; //1000000 const fiveK = 5_000 // 5000 const justLoveUnderscores = 1_2_3.3_2_1 //123.321 const oops = 1__0 // Error: Only one underscore is allowed as numeric separator const nope = 0_1 // Error: Numeric separator can not be used after leading 0

Dealing with binary numbers instead? No worries, you can write it like this:

const five = 0b101; // 5 const alsoFive = 0b101; // 5; `b` is case tolerant const max8bit = 0b1111_1111; // 255; You can use separators in any number :-) const lessFive = -0b0101; // -5

Of course, hexadecimal numbers are also a must-have in your arsenal:

const max8bit = 0xff; // 255 const hexNum = -0xabc; // -2748 const max32bit = 0xffff_ffff; // 4294967295; can use separators

And, just so you know, ECMA Script 2015 introduced octals:

const maxBit = 0o7; // 7 const eight = 0o10; // 8 const funnyZero = 0o0; // 0

If so happens and you can't squeeze your integer number in 64bits, you can convert it to BigInt by adding an n to it:

const maxDouble = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff; // Infinity const biggerThanMaxDouble = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn; // 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137215n const bigTen = 10n; // 10n; const bigLess50 = -50n; // -50n

And, there are several truly magic numbers in JavaScript:

(Infinity === Number.POSITIVE_INFINITY - // true Infinity) === Number.NEGATIVE_INFINITY; // true const smallestFraction = Number.EPSILON; // 2.2204460492503130808472633361816e-16 smallestFraction === Math.pow(2, -52); // true

As everyone has that weird cousin, JavaScript numbers have a special number that is literally not a number.

NaN value is a special value, and every single operation with it will result in NaN, including comparison:

NaN === Number.NaN; // false !! Number.isNaN(NaN); // true NaN + 1; // NaN NaN * 15; // NaN NaN / NaN; // NaN 1 * NaN; // NaN

The most convinient way to make a NaN is through a failed type conversion:

parseInt("abc"); // NaN parseInt({}); // NaN parseFloat("1.1.1"); // NaN "abc" * 1; // NaN new Number("abc"); // Number {NaN} Math.abs("abc");

However, there are built-in functions to help you deal with edge cases and NaN:

Number.isNaN(123); // false Number.isNaN("abc"); // true Number.isNaN(NaN); // true Number.isFinite(123); // true Number.isFinite(Infinity); // false Number.isFinite(NaN); // false Number.isInteger(123); // true Number.isInteger(9007199254740992); // true !! Number.isInteger(12.3); // false Number.isInteger(Infinity); // false Number.isInteger(NaN); // false Number.isSafeInteger(123); // true Number.isSafeInteger(9007199254740992); // false Number.isSafeInteger(12.3); // false Number.isSafeInteger(Infinity); // false Number.isSafeInteger(NaN); // false // BigInts are not considered Integers: Number.isInteger(1n); // false Number.isSafeInteger(1n); // false // But it is a number: Number.isNaN(1n); // false

Thirsty for more? Check out MDN Lexical grammar article.