0

I am about to do some calculating with JavaScript. How do one write (rather complex) mathematical expressions in JavaScript?

Let's take this as an example, where I use x as a variable:

-2(10^ -10)x^6

Is it possible to write it just like that? Or should I write something like this:

-2 * math.pow(10, -10) * math.pow(x, 6) ?

Thanks!

JakeTheSnake
  • 2,456
  • 3
  • 14
  • 26
  • 2
    if you are interested in using that notation for readability look at: http://mathjs.org/ which allows for an eval call on this type of notation. – Cristian Cavalli May 30 '15 at 20:22

2 Answers2

0

The second way is correct. (but require capital m eg Math)

In JavaScript ^ is XOR and * is required for multiplication.

Bob
  • 523
  • 4
  • 11
  • 1
    Tried it that way again, but it did nor work. I found the problem though! A upper case "M" is required! So "Math.pow(10, -10)", not "math.pow(10, -10)" – JakeTheSnake May 30 '15 at 20:26
  • Easy to miss little mistakes like those when you assume something is correct. Glad you figured it out. – Bob May 30 '15 at 20:31
0

-2(10^ -10)x^6

The above statement will never work. It will give you an unexpected identifier because of )x and 2 is not a function because of -2(..

-2 * math.pow(10, -10) * math.pow(x, 6)

This will also won't work as its Math and not math as Javascript is case-sensitive.

Proper way is

-2 * Math.pow(10, -10) * Math.pow(x, 6)
Zee
  • 8,420
  • 5
  • 36
  • 58