JS Fundamentals - Basic Operators, Maths Flashcards
Math Operations
The following math operations are supported:
Addition +, Subtraction -, Multiplication *, Division /, Remainder %, Exponentiation **.
String concatenation with binary +
let s = "my" + "string"; alert(s); // mystring
automatic conversion and concatenation
alert( ‘1’ + 2 ); // “12”
alert( 2 + ‘1’ ); // “21”
complex example where not all operands are converted to strings
alert(2 + 2 + ‘1’ ); // “41” and not “221”
Here, operators work one after another. The first + sums two numbers, so it returns 4, then the next + adds the string 1 to it, so it’s like 4 + ‘1’ = ‘41’.
One more
alert(‘1’ + 2 + 2); // “122” and not “14”
Here, the first operand is a string, the compiler treats the other two operands as strings too. The 2 gets concatenated to ‘1’, so it’s like ‘1’ + 2 = “12” and “12” + 2 = “122”.
Numeric conversion, unary +
The plus + exists in two forms: the binary form that we used above and the unary form.
The unary plus or, in other words, the plus operator + applied to a single value, doesn’t do anything to numbers. But if the operand is not a number, the unary plus converts it into a number.
For example:
// No effect on numbers let x = 1; alert( +x ); // 1
let y = -2;
alert( +y ); // -2
// Converts non-numbers alert( +true ); // 1 alert( +"" ); // 0 It actually does the same thing as Number(...), but is shorter.
Binary plus
let apples = "2"; let oranges = "3";
alert( apples + oranges ); // “23”, the binary plus concatenates strings
let apples = "2"; let oranges = "3";
// both values converted to numbers before the binary plus alert( +apples + +oranges ); // 5
// the longer variant // alert( Number(apples) + Number(oranges) ); // 5
Postfix operator
let counter = 1; let a = counter++; // (*) changed ++counter to counter++
alert(a); // 1
In the line (*), the postfix form counter++ also increments counter but returns the old value (prior to increment). So, the alert shows 1.
when to use prefix and when to use postfix
If we’d like to increase a value and immediately use the result of the operator, we need the prefix form:
let counter = 0;
alert( ++counter ); // 1
If we’d like to increment a value but use its previous value, we need the postfix form:
let counter = 0; alert( counter++ ); // 0
Bitwise operator
AND ( & ) OR ( | ) XOR ( ^ ) NOT ( ~ ) LEFT SHIFT ( << ) RIGHT SHIFT ( >> ) ZERO-FILL RIGHT SHIFT ( >>> )
Comma operator
The comma operator allows us to evaluate several expressions, dividing them with a comma ,. Each of them is evaluated but only the result of the last one is returned.
For example:
let a = (1 + 2, 3 + 4);
alert( a ); // 7 (the result of 3 + 4)
Comma has a very low precedence
Please note that the comma operator has very low precedence, lower than =, so parentheses are important in the example above.
Without them: a = 1 + 2, 3 + 4 evaluates + first, summing the numbers into a = 3, 7, then the assignment operator = assigns a = 3, and the rest is ignored. It’s like (a = 1 + 2), 3 + 4.
why do we need comma operator
// three operations in one line for (a = 1, b = 3, c = a * b; a < 10; a++) { ... }