This new ES6 feature made my math 3 times easier
They brag a lot.
“But 5 lines of Java is one line of Python!”
My Python-loving friends.
They always like to trash languages they stubbornly believe are verbose.
I came to see that “Pythonic” is something truly cherished by my friends in the Python community.
Your Python code works, and so what?
Where is elegance? Where is readability?
Think you can write a simple for loop and get away with it?
total = 0
for i in range(1, 11):
total += i
print("The sum of the first 10 numbers is:", total)
Just wait till one of them find out.
To say you’ll face severe criticism is an understatement.
Because apparently – and I kind of agree – it’s just not beautiful or concise enough.
To be “Pythonic” is best.
total = sum(i for i in range(1, 11))
print("The sum of the first 10 numbers is:", total)
So, which ES6 feature?
The **
operator.
When we debate about conciseness of language this one almost always comes up in Python’s favor.
Up there with generators and the //
operator.
It’s good to know JavaScript now has this feature. Over 8 years ago in fact!
But I was surprised that a sizeable number of my fellow JavaScripters never knew it’s in the language.
Getting the power of a number is a breeze with **
. Introduced in ES6 – 2015.
Instead of Math.pow(a, b)
, you do a ** b
.
const result = Math.pow(10, 2);
console.log(result); // 100
const result2 = Math.pow(2, Math.pow(3, 2));
console.log(result2);
const result3 = 10 ** 2;
console.log(result3); // 100
const result4 = 2 ** 3 ** 2;
console.log(result4) // 512
We don’t need a function for such a common math operation anymore.
You can even pass a decimal number as a power with **
– Math.pow()
can do this too:
const result = Math.pow(49, 1.5);
console.log(result); // 343
const result2 = 49 ** 1.5;
console.log(result2); // 343
And it’s not only a drop-in replacement for Math.pow()
;
**
can take BigInts too
// ❌ Error: Cannot convert a BigInt value to a number
const result1 = Math.pow(32n, 2);
console.log(result1);
const result2 = 32n ** 2n;
console.log(result2); // 1024n
In case you don’t know what BigInts are in JavaScript:
they’re simply a way for us to represent numbers of any size.
Without losing precision or experiencing overflow errors.
const veryLargeNumber = 1234567890123456789012345678901234567890n;
console.log(typeof veryLargeNumber); // "bigint"
console.log(veryLargeNumber * 2n); // 2469135780246913578024691357802469135780n
You can see that we simply add an n
at the end of the digits to make it a BigInt.
Final thoughts
Language wars are a fun programmer pastime.
To wrap things up, it’s always fun to debate about which programming language is more elegant and concise.
But at the end of the day, it’s important to keep in mind that writing readable and maintainable code should always be a top priority.
In this article, we saw that the ** operator introduced in ES6 for JavaScript is a neat trick that can make your code more concise, and it even works with BigInts!
More features keep getting added every year – ES13 was released in 2022 – to increase and add more syntactic sugar.
So, keep exploring the possibilities of your favorite programming language, and have fun coding!