Arithmetic Operators

🙋 Need help? Ask an expert now!

Arithmetic operators +, -, *, / are used in JavaScript the same way they as used in math. The division operator, however, not only returns numbers, but also words such as "Infinity" or "NaN". Infinity is usually returned when you divide a number by zero while NaN is returned if you try zero divided by zero. Division in JavaScript returns a floating-point number, and are not automatically rounded into an integer.

An arithmetic operator you may not be familiar with is the remainder operator, or the modulus operator. The remainder operator % returns the remainder from the division of numbers, even if they are decimals. For example, "5 % 2" will return 1 as the remainder of five divided by two is one. Other special operators include the increment and decrement operator, which increments/decrements a variable's value by one.

The sample code above show a variety of variables as well as arithmetic operators and how they function. Incrementing and decrementing a variable (b++) is actually the same as doing b = b + 1, but with less effort. As explained above, dividing any number by zero will result in positive or negative infinity based on the sign of the first operand. Zero divided by zero will return NaN, which stands for "Not a number". Using the remainder operator with a negative number and a positive number will result in the a negative value for the remainder, as shown with console.log(-11%3).

Edit Me on GitHub!

Application Question

Consider the following code segment:

let varOne = 12 % 3.5;

What will the following code print to the console?