7 ms·
TIL You can write javascript numbers with underscore separators https://v8.dev/features/numeric-separators https://v8.dev/features/numeric-separators
by roberttod 5y ago
TIL You can write javascript numbers with underscore separators https://v8.dev/features/numeric-separators https://v8.dev/features/numeric-separators
- codetrotter 5y agoSame in Python 3 and Rust too. https://www.python.org/dev/peps/pep-0515/ https://www.python.org/dev/peps/pep-0515/ https://doc.rust-lang.org/rust-by-example/primitives/literals.html https://doc.rust-lang.org/rust-by-example/primitives/literal...
- omegalulw 5y agoInterestingly, why do they specify base 10 integers as: nonzerodigit (["_"] digit)* | "0" (["_"] "0")* Isn't digit (["_"] "0")* simpler?
- Jasper_ 5y ago012345 is the old way of doing octal notation in Python, which has its roots in C. I'm guessing they want to prevent ambiguity by making it so that it's an error for "012345" to be a decimal literal.
- dr_zoidberg 5y agoYes, it gives an error -- if you want decimals, you can't use leading 0's: Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] Type 'copyright', 'credits' or 'license' for more information IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: 01 File "<ipython-input-1-3351d58b1d3b>", line 1 01 ^ SyntaxError: invalid token In [2]: 0o1 Out[2]: 1 In [3]: 0o10 Out[3]: 8
- nicoburns 5y agoNot only that, octal notation is actually valid in non strict-mode versions of JavaScript (which is still the default unless you opt-in with "use strict"). So it would be backwards incompatible outside of strict mode, and very confusing if changing from non-strict to strict-mode changed the semantics without giving an error/warning.
- tdeck 5y agoI always thought this was one of the worst notation conventions in modern languages.
- Gibbon1 5y agoI've felt for 35 years they could do everyone a favor and break everything that dependens on a leading zero to represent octal.
- de6u99er 5y agoAnd Java since Java7 which was released in 2011.
- nayuki 5y agoNote that different languages have slightly different syntax rules. 0_0: Legal in Java, legal in Python, illegal in JavaScript. 0__0: Legal in Java, illegal in Python, illegal in JavaScript. 0x_0: Illegal in Java, legal in Python, illegal in JavaScript. 0_, 0_.0, 0._0: Illegal in all three. It wouldn't surprise me if C# and C++(14) have differing edge cases too.
- chrismorgan 5y ago0_0, 0__0, 0x_0, 0_ and 0_.0 are legal in Rust. 0._0 is the only one of those that’s illegal (“`{integer}` is a primitive type and therefore doesn't have fields”).
- Zababa 5y agoTo clarify the special case in Rust ( 0._0 ), it's due to te definition of identifiers: https://doc.bccnsoft.com/docs/rust-1.36.0-docs-html/reference/identifiers.html https://doc.bccnsoft.com/docs/rust-1.36.0-docs-html/referenc.... _0 is an identifier, as it's an underscore followed by one alphanumeric character. Thus, 0._0 means "the field _0 of 0". Since it's an identifier starting with _, you won't get any warnings if you don't use it. Small example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b91364d3e27acda95c094f8cedbb71a4 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- austinjp 5y agoO_o
- scns 5y agoInterestin, i saw it first in Kotlin.
- banana_giraffe 5y agoAnd C# since 7.0 https://github.com/dotnet/csharplang/blob/main/proposals/csharp-7.0/digit-separators.md https://github.com/dotnet/csharplang/blob/main/proposals/csh... First time I saw it was in C#, I love it personally
- pa7ch 5y agoAnd go since 1.13 https://golang.org/doc/go1.13#language https://golang.org/doc/go1.13#language
- kevin_thibedeau 5y agoIt is lifted from Ada. A language ahead of its time.
- thewakalix 5y agoAnd Haskell with the NumericUnderscores GHC extension. https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/numeric_underscores.html https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/nume...
- deleted 5y ago[deleted]
- sarreph 5y agoYou can also write JavaScript numbers with E notation (i.e. multiplied by 10 to the power of x), which is basically what I do with all numbers above 1,000 or below 0.001 now. It has the rare benefit, for big / small numbers, of being both more readable and more concise. Example: 1e3 === 1000; 1e-3 === 0.001; 45.2e6 === 45200000;
- magoghm 5y agoAnd also in Ruby.
- jraph 5y agoD has had this for quite some time, C++ now too (with quote characters instead of underscores)
- lucb1e 5y agoSince 2019 in the leading browsers (Firefox and Chromium); 2020 if you use something else like Edge or Samsung Internet; 2021 for Chrome or Firefox on mobile... and of course you can forget about MSIE or Opera Mobile support as usual. https://caniuse.com/mdn-javascript_grammar_numeric_separators https://caniuse.com/mdn-javascript_grammar_numeric_separator...