The ** operator is overloaded for two types of operands: number and BigInt. It first coerces both operands to numeric values and tests the types of them. It performs BigInt exponentiation if both operands becomes BigInts; otherwise, it performs number exponentiation. A TypeError is thrown if one operand becomes a BigInt but the other becomes a number.
Open-source developer with 15+ years of experience, active Polish Wikipedia editor, mentor, and official Wikimedia Polska trainer. Lead creator of the WikiZEIT project and photographer.
I'm a Software Engineer Passionate about Clean Code, Design Patterns, and System Design. Learning something new every day.
Feel free to say Hi on LinkedIn at https://www.linkedin.com/in/mayallo
Also good to know, contrary to what some people may think, the ^ operator isn't exponentiation at all (it is sometimes used as such, for example in the google calculator), its a XOR operator and will yield totally different results!
I'm a Software Engineer Passionate about Clean Code, Design Patterns, and System Design. Learning something new every day.
Feel free to say Hi on LinkedIn at https://www.linkedin.com/in/mayallo
Never knew they added a Math function for exponents. IMO using ** is better since its like that in other langues like Python, Ruby and probably others. Though Math.pow() is also used in Java and they dont have **
I'm a Software Engineer Passionate about Clean Code, Design Patterns, and System Design. Learning something new every day.
Feel free to say Hi on LinkedIn at https://www.linkedin.com/in/mayallo
I'm a Software Engineer Passionate about Clean Code, Design Patterns, and System Design. Learning something new every day.
Feel free to say Hi on LinkedIn at https://www.linkedin.com/in/mayallo
He/Him; Senior Software Developer, IT Swiss-army-knife.
Lots of coding, some hardware, some devops & sysops, some micro-controller electronics.
I used Arch BTW :)
Anti-GenAI
There is one big difference between them.
Math.powonly works for regular numbers.**also works forBigInts.To quote the MDN page on **:
You're wrong, there is one big difference
Math.pow()works only on number but**operator works also for BigInt.Yes you are right, I meant a big difference for the daily basis work.
Anyway, I am going to add this tip.
Nice!
Also good to know, contrary to what some people may think, the
^operator isn't exponentiation at all (it is sometimes used as such, for example in the google calculator), its a XOR operator and will yield totally different results!Thanks, good tip!
Never knew they added a Math function for exponents. IMO using
**is better since its like that in other langues like Python, Ruby and probably others. ThoughMath.pow()is also used in Java and they dont have**Agree with you,
**is better and easier.Thanks for sharing - I knew about
Math.pow(), but not about**.Glad to know it helps!
Also of note would be that performance wise these two methods seem to be equal (within a small margin)
Math.pow vs ** vs *
Neat!