This new ES7 feature made my math 3 times easier

Last updated on August 07, 2023
This new ES7 feature made my math 3 times easier

But 5 lines of Java is one line of Python.

How many times have you heard something like that from lovers of the later?

Seems like they love to trash languages they stubbornly believe are verbose. I came to see that “Pythonic” is something truly cherished by our 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)

An ES7 feature that brings syntactic sugar and conciseness

The ** operator.

This one almost always comes up in Python’s favor when talking about language conciseness, up there with generators and the // operator.

It’s good to know JavaScript now has this feature, over 6 years ago in fact.

But it was surprising to know that a sizeable number of our fellow JavaScripters never knew it’s in the language.

It’s now effortless to get the power of a number, with the ** operator. 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

BigInts let us 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.

It’s always fun to debate about which programming language is more elegant and concise.

But at the end of the day, we’ve got to keep in mind that writing readable and maintainable code is what matters most.

In this article, we saw that the ** operator introduced in ES7 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!

See also