arithmetic operators javascript code example

Example 1: js calculate

answer_Add = 5 + 1; //answer = 6
answer_Sub = 5 - 1; //answer = 4
answer_Multi = 5 * 5; //answer = 25
answer_Divi = 10 / 2; //answer = 5
Total = answer_Add + answer_Sub + answer_Multi + answer_Divi;

Example 2: javascript Arithmetic operators

var additionOutput = 2 + 2; //addition
var subtractionOutput = 2 - 2; //subtraction
var multiplcationOutput = 2 * 2; //multiplcation
var divisionOutput = 2 / 2; //division
var exponentiation = 2**2; // Exponentiation
var modulus = 5 % 2; // Modulus (Remainder)
var unaryOperator = 1; 
++unaryOperator; // Unary Operator Increment // ++ , --

Example 3: javascript modulo

x = a % b // % is modulo

Example 4: what does -= mean in JavaScript

/* JavaScript shorthand -=
-= is shorthand to subtract something from a
variable and store the result as that same variable.
*/

// The standard syntax:
var myVar = 5; 
console.log(myVar) // 5
var myVar = myVar - 3;
console.log(myVar) // 2

// The shorthand:
var myVar = 5;
console.log(myVar) // 5
var myVar -= 3;
console.log(myVar) // 2