What Does ** Do in JavaScript?

In JavaScript, "**" represents the exponentiation operator, which returns the result of raising the first operand to the power of the second operand:

// ES7+
console.log(2 ** 10); // 1024

This is the same as using the Math.pow() method:

console.log(Math.pow(2, 10)); // 1024

However, one big difference between Math.pow() and ** is the fact that the latter works with bigints as well (as opposed to the former that does not work with bigint values):

// ES10+
console.log(2n ** 10n); // 1024n
// ES10+
// TypeError: Cannot convert a BigInt value to a number
console.log(Math.pow(2n, 10n));

The "n" at the end of a number merely suggests that the number is a bigint primitive.

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.