Exponentiation Operator in JavaScript

Exponentiation Operator in JavaScript

I always found the old way of writing an exponentiation expression a bit awkward. Luckily, the exponentiation operator was introduced. This syntax also makes it more similar to other languages (ie. Ruby, Python). Cool 👍

I always found the old way of writing an exponentiation expression a bit awkward. Luckily, the exponentiation operator was introduced. This syntax also makes it more similar to other languages (ie. Ruby, Python). Cool 👍

// Old way
const old = Math.pow(3, 7);
// 2187

// ✅ ES7 way 
const es7 = 3 ** 7;
// 2187

Infix Notation

The use of ** is called infix notation. It is characterized by the placement of operators between operands. Other popular infix notations include: + or -.

The reason this syntax was introduced is because:

Infix notation is more succinct than function notation, which makes it more preferable## Exponentiation in Other Languages

Also, you will notice this syntax is very similar to other languages:

// Python
a ** b

// Ruby
a ** b

// Perl
a ** b

// F#
a ** b

I actually like that it's similar to other languages. Because it makes picking up JavaScript a lot of easier for those folks and they can be up and running very quickly.

Assignment Operator

You must have seen arithmetic operator combined with the assignment operator. For example +=:

a += b

// Same as
// a = a + b

Well, similarly, this can also be done with the exponentiation operator. **=:

a **= b

// Same as
// a = a ** b

Negative Base

There's one bit of a gotcha. When you have a negative base, you will have to wrap it around parenthesis.

// ❌ Syntax Error
const wrong = -3 ** 7;

// ✅
const correct = (-3) ** 7;

However, this isn't an issue if you use the older function way.

const works = Math.pow(-3, 7);

Resources

javascript web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Web Developer

Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.