Mark As Completed Discussion

Math in JavaScript

Math operations in JavaScript can be performed by directly specifying a mathematical statement. If you're using any variables in the calculation, make sure to initialize them beforehand.

JAVASCRIPT
1var a = 20;
2var b = 10;
3
4console.log(a + b); // prints 30
5console.log(a - b); // prints 10
6console.log(a * b); // prints 200

You can also save these values in further variables, such as:

JAVASCRIPT
1var a = 20;
2var b = 10;
3var c = a + b;
4console.log(c); // prints 30

This is helpful in situations where you need to do more complex calculations, which may require multiple steps.