0

Very simple (stupid?) question: are there built-in arithmetic functions in JS such as addition or multiplication?

For example some imaginary Math.add which can be used like this:

[ 3, 1, 2 ].reduce( Math.add ) // 6
6
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Apr 12, 2019 at 13:46
  • 2
    unfortunately, there's no such thing. Commented Apr 12, 2019 at 13:47
  • 1
    Most arithmetic functions can be expressed in only a few characters. So if you want you, can put all of these in an utility library if you so desire. (a,b)=>a+b, (a,b)=>a*b etc Commented Apr 12, 2019 at 13:49
  • @Shilly, yeah, i know. Just asking in case i'm not aware of some JS features. We can use functions like Number or Boolean to filter or map arrays, so i got curious if we can use some other built-in functions in order to commit some other operations using no custom functions at all Commented Apr 12, 2019 at 13:51
  • 1
    You could write one yourself: const addNumbers = (...arr) => arr.reduce((a, b) => a + b) And call with addNumbers(1,2,3,4) Commented Apr 12, 2019 at 13:52

2 Answers 2

1

No but you could very easily define this yourself:

const add = (x, y) => x + y;
[ 3, 1, 2 ].reduce( add );
Sign up to request clarification or add additional context in comments.

Comments

0

hi lucifer63 the function you are asking for like add, sum might not be available but that you can do by the operator but for the little complex operation we do have

  • For Example Math.round(4.7); Math.PI; Math.pow(8, 2); Math.sqrt(64);

if you check out the w3school Link you can find more math function's which are also very usefull

2 Comments

Great. Now can we combine some of these methods to get some kind of addition function?
@lucifer63 the answer provided by benjaminjosephw const add = (x, y) => x + y; [ 3, 1, 2 ].reduce( add ); can be your solution because using different Math library function to create an add (sum) might not be suitable (or even possible)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.