Arithmetic in JavaScript
by danielkwood
HTML
<html>
<head>
<title>Arithmetic</title>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
JavaScript
// Arithmetic operators
var x = 10; // assignment
console.log(10+5); // addition
console.log(10-5); // subtraction
console.log(10/5); // division
console.log(10*5); // multiplication
console.log(5%2); // modulus (remainder of division)
x = x + 1; // increase x by 1 (long method)
console.log(x);
x++; // increment (increase x by 1)
console.log(x);
x--; // decrement (decrease x by 1)
console.log(x);
x += 5; // compound addition (increase x by 5 - short method)
console.log(x);
x -= 5; // compound subtraction
console.log(x);
x *= 5; // compound multiplication
console.log(x);
x /= 5; // compound division
console.log(5**2); //exponentiation (raises 5 to power of 2)
console.log(Math.pow(5,2)); // using exponentiation function
console.log(Math.sqrt(4)); // square root
console.log((5 + 10) / 2); // grouping numbers using parantheses