To work with variables, with values, JavaScript supports all standard operators, most of which are in other programming languages.
Terms: “unary”, “binary”, “operand”
Operators have their own terminology, which is used in all programming languages.
- The operand is what the operator applies to. For example:
5 * 2
- multiplication operator with left and right operands. Another name: operator argument. - Unary is an operator that is applied to a single expression. For example, the operator unary minus
"-"
changes the sign of a number to the opposite: - Binary is the operator that is applied to two operands. The same minus exists in binary form:
The operation of the unary "+"
and binary "+"
in JavaScript is significantly different.
These are really different operators. Binary plus adds operands, and unary - does nothing in arithmetic terms, but it leads the operand to a numeric type. Next we will see examples.
Arithmetic operators
The basic arithmetic operators have been familiar to us since childhood: this is plus +
, minus -
, multiply *
, divide /
.
For example:
Or a bit more complicated:
The rarer arithmetic operator %
interesting because it has nothing to do with interest. Its result, a % b
, is the remainder of dividing a
by b
.
For example:
Addition of lines, binary +
If the binary operator +
applied to strings, then it combines them into one:
var a = "моя" + "строка" ; |
If at least one argument is a string, the second one will also be converted to a string!
And it does not matter whether the operand string is on the right or left, in any case the non-string argument will be converted. For example:
This cast to a string is a feature of the binary operator "+"
.
The remaining arithmetic operators work only with numbers and always bring the arguments to a number.
For example:
Unary plus +
Unary plus as an arithmetic operator does nothing:
As you can see, plus did not change anything in expressions. The result is the same as without it.
Nevertheless, it is widely used, since its “side effect” is the conversion of a value into a number.
For example, we have two numbers, in the form of strings, and we need to add them. Binary plus adds them up as strings, so we use unary plus to convert to a number:
Assignment
The assignment operator looks like the equals sign =
:
It evaluates the expression that is on the right, and assigns the result to a variable. This expression can be quite complex and include any other variables:
In line (*)
, a calculation will first be performed using the current value of a
(i.e. 1
), after which the result will overwrite the old value of a
.
Possible assignment on the chain:
Such an assignment works from right to left, that is, the rightmost 2+2
expression is calculated first, assigned to c
, then b = c
and, finally, a = b
.
All operators return value. A call to x = выражение
writes the expression to x
, and then returns it. Because of this, the assignment can be used as part of a more complex expression:
In the example above, the result (a = b + 1)
is a value that is written to a
(i.e. 3
). It is used to calculate c
.
A fun use of assignment, right?
To know how it works is necessary, but to write oneself only if you are sure that it will make the code more readable and understandable.
A priority
If there are several operators in the expression, the order of their execution is determined by the priority .
From school we know that the multiplication in the expression 2 * 2 + 1
will be executed before addition, since its priority is higher, and parentheses explicitly specify the order of execution. But in JavaScript, there are much more operators, so there is a whole table of priorities.
It contains both already passed operators, and those that we have not yet passed. In it, each operator is given a numerical priority. Anyone who has a lower number will be executed earlier. If the priority is the same, the execution order is from left to right.
Excerpt from the table:
... | ... | ... |
five | multiplication | * |
five | division | / |
6 | addition | + |
6 | subtraction | - |
17 | assignment | = |
... | ... | ... |
Let's look at the table in action.
In the expression x = 2 * 2 + 1
there are three operators: assignment =
, multiplication *
and addition +
. The multiplication priority *
is 5
, it will be executed first, then there will be an addition +
, whose priority is 6
, and after them - assignment =
, with priority 17.
Increment / Decrement: ++, --
One of the most frequent operations in JavaScript, as in many other programming languages, is to increment or decrement a variable by one.
There are even special operators for this:
- Increment
++
increases by 1: - Decrement
--
reduces by 1:
Increment / Decrement can only be applied to a variable.
Code 5++
will give an error.
You can call these operators not only after, but also before a variable: i++
(called “postfix form”) or ++i
(“prefix form”).
Both of these forms of recording do the same thing: increase by 1
.
However, there is a difference between them. It is visible only in the case when we want not only to increase / decrease the variable, but also to use the result in the same expression.
For example:
In line (*)
call to ++i
will increment the variable, and then return its value to a
. That is, a
gets the value of i
after increasing .
The postfix form i++
differs from the prefix ++i
in that it returns the old value that was before the increase.
In the example below, a
gets the old value of i
, equal to 1
:
- If the result of the operator is not used, it is only necessary to increase / decrease the variable - no matter what form to use:
- If you want to immediately use the result, then you need a prefix form:
- If you need to increase, but you need the value of the variable before increasing - the postfix form:
Increment / Decrement can be used in any expression.
At the same time, it has a higher priority and is executed earlier than arithmetic operations:
At the same time, it is necessary to use such a record with caution, because when reading a code it is often not obvious that changing increases. Three lines are longer, but clearer:
Importance: 5
[Open task in new window]
Bitwise operators
Bitwise operators treat arguments as 32-bit integers and operate at the level of their internal binary representation.
These operators are not JavaScript-specific, they are supported in most programming languages.
The following bitwise operators are supported:
- AND (i) (
&
) - OR (or) (
|
) - XOR (bitwise exclusive or) (
^
) - NOT (not) (
~
) - LEFT SHIFT (left shift) (
<<
) - RIGHT SHIFT (right shift) (
>>
) - ZERO-FILL RIGHT SHIFT (right shift with padding with zeros) (
>>>
)
You can read more about them in a separate article, Bitwise Operators.
Calling Assigned Operators
Often you need to apply an operator to a variable and save the result in it, for example:
This record can be shortened using combined operators: +=, -=, *=, /=, >>=, <<=, >>>=, &=, |=, ^=
.
Like this:
All of these operators have exactly the same priority as regular assignment, that is, they are executed after most other operations.
Importance: 3
Answer: x = 5
.
The assignment operator returns the value that will be written to the variable, for example:
Hence x = 1 + 4 = 5
.
[Open task in new window]
Operator comma
Comma is also an operator. It can be called explicitly, for example:
A comma allows you to list expressions, separating them with a comma ','
. Each of them is calculated and discarded, with the exception of the last, which is returned.
A comma is the only operator whose priority is lower than assignment. In the expression a = (5,6)
, the parentheses are used to explicitly set the priority, otherwise the operator '='
would execute to the comma ','
, it would turn out (a=5), 6
.
Why do we need such a strange operator, which discards the values of all the listed expressions, except the last one?
Usually it is used in the composition of more complex structures to do several actions in one line. For example:
2 | for ( a = 1, b = 3, c = a*b ; a < 10; a++) { |
Such tricks are used in many JavaScript frameworks to shorten code.
Comments
To leave a comment
Scripting client side JavaScript, jqvery, BackBone
Terms: Scripting client side JavaScript, jqvery, BackBone