- 1. Pipeline Operator
- 2. Optional Chaining
- 3. Nullish Coalescing
- 4. Logical Assignment
- Show Me the Code
Babel 7.0 was released this week summing up two years of work and 4000 commits (yes, that’s 4000!).
1. Pipeline Operator
const mul = x => y => x*y const result = 42 |> mul(2) |> console.log
Which “pipes” the number 42 through a multiplication and a log operation. Note that
mul is a multiplication function in its curried form. Currying as a concept fits extremely well when we're dealing with functional programming (and now you can see why).
If you’re not using Babel (say you’re on Typescript) you can still have a similar experience today with Ramda’s pipe function or the Lodash flow function, or if you prefer Pyton then the toolz pipe function.
2. Optional Chaining
The optional chaining operator (‘?’) tucked onto properties will make chaining access not explode, and will carry on an ‘undefined’ value if found, up to the last property access.
This makes it easy to perform the same operation without any edge case logic, and avoid bad input
Optional chaining is one of the solutions for the “null pointer exception” problem — a pain that has a long history in programming languages, even at one point named “ The Billion Dollar Mistake” — is the reputation the null value got.
3. Nullish Coalescing
The nullish coalescing operator (‘??’) lets you provide a default value if left-hand is nullish (undefined, null, etc.) and so compared to the previous example will never return undefined values.
4. Logical Assignment
The logical assignment operator (‘||=’, ‘&&=’), inspired by Ruby and CoffeeScript sprinkles a little syntax sugar for when you want default values semantics but with a concise form of code.
Granted, for some this semantic matter more, and for some it matters less. For those who’s doing immutable code (functional programming) it will matter less.### One More Thing…
The upcoming pattern matching proposal is the one I’m the most excited about and been following for around a year now.
The next one I’m excited about is the partial application proposal which means you can bake half functions (curry) to be used in pipelines very easily.
Show Me the Code
You can play with all of these today by cloning the Github repo here.
If you want to use the new features today in production, you can use my babel mega-configuration repo which make all of Babel’s configuration seamless and transparent for you.