JS Fundamentals - Basic Operators, Maths Flashcards

1
Q

Math Operations

A

The following math operations are supported:

Addition +,
Subtraction -,
Multiplication *,
Division /,
Remainder %,
Exponentiation **.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

String concatenation with binary +

A
let s = "my" + "string";
alert(s); // mystring
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

automatic conversion and concatenation

A

alert( ‘1’ + 2 ); // “12”

alert( 2 + ‘1’ ); // “21”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

complex example where not all operands are converted to strings

A

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’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

One more

A

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”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Numeric conversion, unary +

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Binary plus

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Postfix operator

A
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

when to use prefix and when to use postfix

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Bitwise operator

A
AND ( & )
OR ( | )
XOR ( ^ )
NOT ( ~ )
LEFT SHIFT ( << )
RIGHT SHIFT ( >> )
ZERO-FILL RIGHT SHIFT ( >>> )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Comma operator

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Comma has a very low precedence

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

why do we need comma operator

A
// three operations in one line
for (a = 1, b = 3, c = a * b; a < 10; a++) {
 ...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly