What are operators

The operator is usually a symbol that is used when we want to perform some standard, frequently used operation, such as assignment of values, comparison of values, arithmetic operations…
Operators can be understood as special functions which, unlike “ordinary functions”, are written in a “non-standard way”. Operators use the so-called “infix” notation (the function name is placed between two parameters). The standard operator shown with “infix” notation looks like this:
... operator...
The following example shows that there is an essential similarity between the operator and the function:
|
1 2 3 4 |
function plus (a, b) { return a+b; } plus(3,4); // Returns7 |
The most common are binary operators (operators that take two values as input), although there are also unary operators that perform certain operations on only one value:
|
1 2 3 |
var brojac = 0; brojac++; console.log(brojac); // Returns: 1 |
Priority and associativity of operators
The associativity of the operator is a property that tells from which end the grouping takes place (starting from the left end or from the right end). Each operator has its own associativity rule. In the followingtable of associativity and importance of operators operators and their associativity are shown, and they are ranked according to “importance” from “most important” to the weakest.
“undefined” & “null” as comparators!?
Comparing undefined to numbers or strings ALWAYS returns FALSE:
|
1 2 3 4 5 6 7 8 9 10 |
undefined > 5 //false undefined < 5 // false undefined == 0; //false undefined == ""; //false undefined > 0; //false undefined < 0; //false undefined > -1; //false undefined < 1; //false undefined >= 0; //false undefined <= 0; //false |
Comparison with “null” is extremely illogical, at the very least strange and without any rules, and such comparisons should be avoided at all, i.e. prevent null from appearing in the comparison!
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
null > 5 //false null < 5 //true null == 0; //false null == ""; //false null > 0; //false null < 0; //false null >= 0; //true null <= 0; //true null > -1; //true null < 1; //true |
And if they are compared to each other
|
1 2 3 4 5 6 |
undefined == null //true null == undefined //true null > undefined //false null <= undefined //false null < undefined //false |
Specificity of “==” operator
Operator “==” so called “weak equality” compares two operands according to their value and returns a logical value of TRUE or FALSE.
Doimplicit type conversion will be performed, it depends on which types are being compared. If the comparison of two values of different types leads to an implicit conversion of one of them, this procedure is performed according to the conversion rules, followed by a comparison. The following table lists all cases between all possible types for comparison with the operator “==”
| Operand B | |||||||
|---|---|---|---|---|---|---|---|
| Undefined | Null | Number | String | Boolean | Object | ||
| Operand A | Undefined | true | true | false | false | false | false |
| Null | true | true | false | false | false | false | |
| Number | false | false | A === B | A === ToNumber(B) | A === ToNumber(B) | A == ToPrimitive(B) | |
| String | false | false | ToNumber(A) === B | A === B | ToNumber(A) === ToNumber(B) | A == ToPrimitive(B) | |
| Boolean | false | false | ToNumber(A) === B | ToNumber(A) === ToNumber(B) | A === B | ToNumber(A) == ToPrimitive(B) | |
| Object | false | false | ToPrimitive(A) == B | ToPrimitive(A) == B | ToPrimitive(A) == ToNumber(B) | A === B | |
Example
When comparing these two types, the rule is to convert string to number
|
1 |
console.log("40" == 40); // => 40 == 40 returns TRUE |
Example
This case often causes problems because some rules in JS can lead to the wrong conclusion. The rule to follow is that when comparing logical value is converted to number, see more here.
|
1 |
console.log("50" == TRUE); // => 50==1 returns FALSE |
Example
According to ES5 comparing undefined == null with the “loose equality” operator ALWAYS returns TRUE.
|
1 2 |
undefined == null; //TRUE null == udefined; //TRUE |
Example
When comparing undefined/null to some primitive data type it ALWAYS returns FALSE.
|
1 |
console.log(2 == null); // ReturnsFalse |
A common misconception is that when comparing undefined/null and some other primitive data type a conversion to FALSE and then to zero is expected, and that after that a comparison is possible.
Specificity of operators && and ||
When one or even both operands are not of logical type when using operators && and ||, conversion to logical type occurs according to conversion rules, but after solving the condition they do not return a logical value, but return the value of one of the two operands. What will be the result of the conditions is determined according to the following rules:
- For the operator || after the conversion of “not logical” type to logical when solving the condition, the following applies:
if the final solution of the operator condition is TRUE, it returns the value of the first operand, and in the case of FALSE, it returns the value of the second operand.
1234console.log(3 || true); //rezultat je 3console.log(3 || false); //rezultat je 3console.log(true || 3); //rezultat je TRUEconsole.log(false || 3); //rezultat je 3 - For the operator && after the conversion of “not logical” type to logical when solving the condition, the following applies:
if the final solution of the operator condition is TRUE, it returns the value of the second operand and vice versa.
1234console.log(3 && true); //rezultat je trueconsole.log(3 && false); //rezultat je falseconsole.log(true && 3); //rezultat je 3console.log(false && 3); //rezultat je false
Examples
Additional conditions
|
1 2 3 4 5 |
var a = 42; var b = "foo"; var c = [1,2,3]; a && b || c // Rezultat je : "foo" |
Explanation:
Respecting the precedence of the operators used, the logical “and” takes precedence over the logical “or”, so the previous expression can be converted to:
|
1 |
(a && b) || c |
In order to determine the conditions in the parentheses, an implicit type conversion is performed to a logical type:
|
1 |
(TRUE && TRUE) || c |
The solution of the condition is TRUE, and therefore according to the mentioned rule for TRUE, the operator “&&” returns the value of the second operand, so the sorted expression looks like this:
|
1 |
"foo" || [1,2,3] |
Implicit conversion to boolean type is done again:
|
1 |
TRUE || TRUE |
The solution to this condition is TRUE, so according to the rule, the operator || returns the value of the first operand.
Continued assignment
|
1 2 3 |
var a = 2, b = 3, c = 4; a = b = c; // Returns: 4 |
Explanation:
The associativity property of the assignment operator “=” is right-to-left so the previous expression could be displayed grouped like this:
|
1 |
a = (b = c); |
Therefore, the value 4 will first be assigned to the variable b, after which the expression becomes:
|
1 |
a = 4; |
3.) Linked comparison
|
1 |
3 < 2 < 1 // ReturnsTRUE |
Explanation:
The associativity of the operator “<" is from left to right so the expression after grouping looks like this:
|
1 |
(3 < 2) < 1 |
Solving the first operation giveslogical value FALSE, so the lookalike expression
|
1 |
FALSE < 1 |
When comparing two elements of different types, there is an implicit conversion of FALSE to the number “0”, after which the expression looks like:
|
1 |
0 < 1 |
Which returns the result TRUE
