Javascript Arithmetic Operators...where Are They?
What is the precise nature of JS's arithmetic operators (+, -, *, /)? I imagine they're functions, but they're not on Number's or String's prototype, and they're definitely not fir
Solution 1:
They're known as infix operators, and they're inbuilt, and act like Function prototypes.
It's interesting to note that regular JavaScript doesn't support things like exponentiation through infix operators, and you're forced to resort to an extension of Math
:
console.log(Math.pow(7, 2));
Although ES6 fixes this:
console.log(7 ** 2);
Although you can't create your own infix operators, you can extend them:
Function.prototype['∘'] = function(f){
returnx =>this(f(x))
}
constmultiply = a => b => (a * b)
const double = multiply (2)
const doublethreetimes = (double) ['∘'] (double) ['∘'] (double)
console.log(doublethreetimes(3));
Hope this helps! :)
Post a Comment for "Javascript Arithmetic Operators...where Are They?"