I'm a software developer with a great interest in data analysis and statistics.
Modulo or remainder
Addition Operator +
The addition operator adds numbers or concatenates strings, depending on the datatypes of the operands.
As you’ve seen in the example above, when the values of both operands are numbers it will be basic arithmetic addition. When both are strings, then it’s also pretty much obvious what the + operator does, namely gluing the operands together, also known as concatenation.
In many other cases, type conversion is needed and the result depends on the position of the operands. The addition operator will set the priority to string concatenation. If one of the operands is a string, then the result will be a string.
If you perform addition on a pair of operands that are of the number type and then you add a string, it will first perform the arithmetic on the numbers and next it will concatenate with the String (“3 eyes” example below).
If you have a number type with the addition operator followed by a string, another addition operator and then a number again, then it will concatenate the first number with the string, then concatenate that result with the last number (“1 eyes 2” example below).
Subtraction Operator –
The subtraction operator works like normal arithmetic. If you subtract and one or more of your operands are strings of non-numeric characters you’ll get NaN (Not a Number) as a result.
If you subtract two strings with only numeric characters, you’ll get a number, like normal subtraction of numbers. If one operand is a string with only numeric characters and the other operand is a number you’ll get a number as a result.
Multiplication Operator *
It takes two operands and multiplies them, just like basic math. If both operands are numerical strings, they will be coerced to numbers (type conversion) and the result will be multiplication of those numbers. The same thing will happen if one operand is of type number and the other is a numerical string.
If one or more operands are non-numerical (words, alphabetical chars,…) strings we get NaN (Not a Number).
Division Operator /
The division operator takes number operands and will return a number, float or “Infinity” (if the later operand is zero) after dividing them. Type conversion will occur for numerical strings. For non-numerical string, we will get NaN, similar to multiplication and subtraction.
Modulo Or Remainder Operator %
This special modulo operator finds the remainder after division of one number of another. Here we also have type conversion for numerical strings and NaN when non-numerical strings are involved.
Increment Operator ++
The increment operator adds 1 to its single operand. The operator will add 1 to the value of the variable (let) and then assigns the result back to the variable.
There are two types of increment operators: pre-increment and post-increment operators. Pre-increment will increment the operand and evaluates to the incremented value immediately. While post-increment will increment its operand but will still evaluate to the unincremented value of that operand when assigned to another variable at the same time.
Decrement Operator – –
The decrement operator will convert the value of the operand to a number (if needed and possible), subtract 1, and assign the result into the operand (variable). Similar to the increment operator, the decrement has two types: pre-decrement and post-decrement.
It is worth to note that the expression ++a is not always the same as a = a + 1. The increment operator and decrement operator always convert the operand first.
© 2019 Sam Shepards