7 ms·
Two Features of JavaScript that you might not know
- Legogris 6y agoI find the linked post about two new features in Node.js 14[0] much more interesting: > Optional Chaining operator (?.) > Nullish Coalescing operator (??) Esp chaining can have a huge impact on readability. [0]: https://www.monkwhocode.com/2020/05/nodejs-v14-2-things-to-know.html https://www.monkwhocode.com/2020/05/nodejs-v14-2-things-to-k...
- eyesee 6y agoOptional chaining is also now supported in JS itself, with growing browser support: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- y-c-o-m-b 6y agoWell now I feel stupid. I was irritated just yesterday at not having this feature available in JavaScript. I should have done a search. Looks like there's a babel plugin too: https://babeljs.io/docs/en/next/babel-plugin-proposal-optional-chaining https://babeljs.io/docs/en/next/babel-plugin-proposal-option...
- leetrout 6y agoAnd JavaScript slowly becomes typescript. Hooray!
- SketchySeaBeast 6y agoI'm honestly hoping to see someone respond to you with why that would be a bad thing. Given my experience with TypeScript and how it makes my life easier, I can't see why anyone would have issue with it, but I know it must exist.
- thomasrognon 6y agoTypeScript is following JavaScript, not the other way around. Generally, ECMAScript proposals get added to TypeScript when they hit stage 3. Optional chaining and nullish coalescing were added to V8/Chrome and TypeScript around the same time.
- brlewis 6y agoYes, TypeScript adds features from the ECMAScript standard. Sometimes JS implementations add them around the same time TypeScript adds them. But sometimes TS adds them much earlier. Having been using TS, it surprised me in this article that nodejs didn't add numeric separators until recently. I had to check MDN to make sure it was right. TS, with or without Babel, keeps me from having to know what features are supported where. I used to frequently check MDN and caniuse.com out of necessity. Now I check infrequently out of curiosity.
- agustif 6y agoOh I've been using this on TS without knowing it had a name. TIL
- mattlondon 6y agoNot just in node.js but browsers too Basic examples for null coalescing here https://byexample.xyz/javascript/ECMAScript2020/nullCoalescing/ https://byexample.xyz/javascript/ECMAScript2020/nullCoalesci...
- throw1234651234 6y agoThis has been in other languages (C#, Java) for ages.
- thiht 6y agoGood for them!
- wdb 6y agoI keep thinking that the optional chaining operator would also work for functions e.g. this.myOptionalfunction?('hello') were I would expect it would only get called when it's defined. But that's not possible, I think they missed a nice addition. Oh looks like the operator is actually '?.' instead of just '?' so guess this.myOptionalFunction?.() could work but that looks really confusing in my eyes.
- Legogris 6y agoI am trying to think of a scenario where this makes sense, but I'm struggling. When do you want to call a function that might exist, and if so, not care about if it exists or not? (Since null or undefined returned from the function call can't be distinguished from the non-presence of the function)
- jlokier 6y agoIt has to be '?.(...)' not '?(...)', because '?(...)' is already part of valid syntax. For example "this.myOptionalfunction?('hello'):('goodbye')".
- filoeleven 6y agoThis has been in Clojurescript since its inception back in 2011 or so, and the two new operators are handled by the same function: (get-in obj [key1 “key 2” (make-key obj 3) 4] “not-found”) There is also a simpler version for single-level access with (get obj key not-found). “not-found” can be a value or a function or a function evaluation, and the keys can be of any type, including nested maps or, if you wanna get really wild, functions. Someday I’ll get to use this professionally instead of transpiling piles of NPM dependencies into something IE11 can work with...
- filoeleven 6y agoReplying to my own comment to keep this properly namespaced: Both of the new additions to JS specified in the article can be sort of done in cljs, though each one will be a function. This already fits with the existing paradigm; there are no operators in Clojure, only functions. “2 + 3” in JS is already “(+ 2 3)” in CLJS. The Math.pow() vs “two stars operator” is kinda pointless: it can be reduced to (pow x y) with a require statement. Is that really used often enough to warrant new syntax? Here’s how big numbers can be separated by space though. It does not handle overflows; I don’t think the JS version does either: (defn bignum [& xs] (js/parseInt (apply str xs))) (bignum 123 456 789) ;; yields 123456789 as a JS int There’s an efficiency argument to be made here, in that a JS parser can do this with less overhead. But if you have that many unreadable big numbers written in your codebase, I think you have bigger problems.
- buraksarica 6y agoThis link contains the biggest header ad I've ever seen. Is it on purpose? Or is it just on my machine?
- SaltyBackendGuy 6y agoSame on my machine. I ended up not reading the article due to the the ads. Generally I'll deal with them to support the content creator, but these were too much for me to deal with.
- adamkl 6y agoA few more items added to the kitchen sink that is JavaScript. JavaScript's multi-paradigm approach is one of its strengths, but, in my opinion, a weakness at the same time. It allows people to write JavaScript following whatever approach suits them, which is fine for individual developers and small teams, but expand beyond that and it becomes a big ask for every developer to be up to speed with every feature JavaScript supports. I use and teach JavaScript/TypeScript every day, and its possible to be very productive with it, but I prefer the approaches of Go and Clojure better. Limited languages that support a single paradigm, and support it well.
- spanhandler 6y agoOne of the goofiest things about JavaScript is how some of its most important and basic features and properties—say, prototypal inheritance and functions-are-objects—fall in the "you should probably never use these in any way that actually leverages their notable capabilities in JS, and if you think you need to, think again, and please... don't" category. The result is that practically no-one wants to write in whatever a "JS native" paradigm might look like. Everyone wants to write it like C, or like Java, or like OCaml, or whatever. Mind, I don't think it's a bad thing people avoid writing "JS native" style, because I certainly don't want to be handed a codebase in which anyone's done anything remotely interesting with prototypal inheritance—it's just something I find interesting about the community of such a popular language.
- throw1234651234 6y agoThis. Though pretentious "experts" push it as "a must know for senior JS developers". Same with closures. Sure, it's nice to know when you create a closure by accident, but using it on purpose? Come on. It's all hacks for implementing "private" modules.
- neurotrace 6y agoClosures are handy for a number of things. Many event listeners are closures const [username, setUsername] = useState('') const onChange = event => setUsername(event.target.value) const onSave = () => dispatch(saveUsername(username)) return ( <div> <input value={username} onChange={onChange} /> <button onClick={onSave}>Save</button> </div> ) This code contains two closures
- sago 6y agotldr: If you don't want to scroll down the massive header and repeated statement of the title: 1. Double asterisk is the exponentiation operator. 2. _ can be used in numbers, e.g. 1_000_000 Also (and not in the article) both are also true in Python.
- Shorn 6y agoTa
- neurotrace 6y agoWhy in the world would you put a near full-page sized ad at the top of the article? I seriously thought that the site had been hijacked at first
- heinrichhartman 6y agomoney
- infinityplus1 6y agoDon't you use an adblocker?
- hnrodey 6y agoI've used this successfully for years. https://github.com/StevenBlack/hosts https://github.com/StevenBlack/hosts While not your traditional ad blocker done via browser extension, it does the job quite well and blocks the requests at the OS level. I'm not the author or maintainer, just a very happy user.
- MacSystem 6y agoI use brave browser
- neurotrace 6y agoI avoid using one on smaller sites so I can support content creators
- speedgoose 6y agoI prefer to support them by sending actual money. I value my privacy and the loading speed of a website.
- wolco 6y agoDid you send money for the article you just read? If not maybe they need ads. So many people say they would rather pay directly but few do when given the chance.
- klodolph 6y agoAs a minor note—the exponentiation operator was added in ES2016 / ES7, not ES2015 / ES6. It wouldn’t matter, except you need to tell all your tools (Terser, etc.) that you are using ES2016 or they will throw syntax errors when processing your code.
- seanalltogether 6y agoIs there a good reason that ^ is not used as an exponent operator in most (all?) languages. Its used in every calculator Ive seen. 2^5 = 32
- pansa2 6y agoC doesn't have an exponentiation operator, and instead uses `^` to mean XOR. Other languages have copied that meaning of `^` and hence need to use something different for exponentiation. However, Lua is one language that does use `^` for exponentiation (its XOR operator is `~`).
- drej 6y agoYes, it's often used for a XOR operation (bitwise exclusive or).
- williamstein 6y agoI speculate that ^ is used for xor in many languages for psychological compatibility with C, which is a very old language. In SageMath, which uses a lightweight preparser on top of Python, I made it automatically convert ^ to , since otherwise mathematicians, who are used to LaTeX, would be very confused (I remember realizing this during a tutorial at Pycon in 2005).
- deleted 6y ago[deleted]
- tobr 6y agoCould someone edit the title? As it stands it could be about literally any two features of JavaScript.
- zachrose 6y agoI was expecting this to be about object literal setters (and getters): const language = { set current(name) { this.log.push(name); }, log: [] }; Supported by browsers for fifteen years now.