Content
Operators in Javascript
JavaScript has a wide variety of operators
These operators can be distinguished into two groups: binaries, which act on two operands and unaries, which only require one operand
Thus, its general syntax is:
operand1 operator_Binary operand2
operand1 operator_unary
operator_unary operand1
Arithmetic operators
JavaScript provides the basic operations with the only operator additive that returns the rest of the division between the left and the right operator. More complex operators are lacking, although the Math object defined in JavaScript has these tasks
Arithmetic operators | |
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Rest |
Note The operator + applied to strings concatenates both strings in one
Operators of increase (++) and decrease (- -)
These operators are unary and perform the self increment and self growth to the variable that applies to them. In addition to modifying the variable, they return the value of it
The operator increment or decrement can go before p behind the variable having a different meaning
If the operator is ++, it is placed after the variable is called post-increment, by first taking the value and then increasing the variable by one unit
If the ++ operator is placed before the variable, it is called pre-increment and makes that first to increase in a unit variable and then take the value
If the operator is - - it is placed after the variable, it is called post-decrement, making it take the value first and then the variable is decremented by one unit
If the operator - - is placed before the variable, it is called pre-decrement and makes that first decremente in a unit variable and then take the value
Relational operators
They are typically used in conditional expressions. Relational operators return Boolean values. The operands can be both numerical and strings
Relational operators | |
> | Greater than |
< | Less than |
> = | Greater than or equal to |
< = | Less than or equal to |
! = | Other than that |
= = | Like |
Logical operators
Logical operands are related to relational ones since normally the operands they use are the result of relational expressions. The resulting values are Boolean
Logical operators | |
& & | AND |
| | | OR |
! | NOT |
Bitwise operators
The way of working of these operators is to convert to the binary operands and then operate with them bitwise
Bitwise operators | |
& | AND |
| | OR |
^ | XOR |
<< | Propagation to the left Shift the value to the left by entering zeros, if it goes out of range, values are lost |
>> | Spread to the right Moves the value to the right entering by the left, the sign bit and eliminating the values that are out by the right |
>>> | Zero fill, right propagation Similar to << but to the right. Do not enter sign bit |
Note Propagation operators take two operands: the first is the variable to propagate and the second is the number of positions to propagate
Assignment operators
The assignment is also an operator that returns the modified variable. The assignment operator in JavaScript is =. The assignment operators shown below are nothing more than abbreviations that make expressions more comfortable and simple, although sometimes they are more illegible
Assignment operators | ||
Operator | Expression | Equivalence |
= | A=B=C; | A=C; B=C; |
+ = | A + = 4; | A = A + 4; |
– = | A – = 3 * B; | A = A – (3 * B); |
* = | A * = 2; | A = A * 2; |
/ = | A / = 35 + B; | A = A / (35 + B); |
>> = | A >> = 1; | A = A >> 1; |
<< = | A << = B; | A = A << B; |
>>> = | A >>> = B + 1; | A = A >>> (B + 1); |
& = | A & = (C + = 3); | C = C +3; A = A & C; |
^ = | A ^ = 2; | A = A ^ 2; |
| = | A | = C; | A = A | C; |
Other operators
Selection operator
This operator is used to execute one operation or another depending on the condition. The format is as follows:
Condition ? Exp1 : Exp2
If the condition is met, the expression Exp1 is returned and if the Exp2 is not. We can put only one value. Example:
New operator
This operator is to be used to create an instance of a previously defined type of object. The syntax to follow is as follows:
variableObject = new ObjectType(parameter 1, parameter 2, …)
These parameters are passed to the constructor of that object in question
Typeof operator
This operator applied to a variable returns the type of object to which the data contained by that variable belongs. Its syntax is:
typeof(variable)
Preference
The preference of the operators will determine the order in which they are executed in a given expression. Using parentheses we will control that the operations are carried out as we want. In JavaScript, the preference of operators from highest to lowest is as follows:
Preference | |||||||||||
Negation / (in / de) crease | ! | ++ | — | ||||||||
Mul / Div / Rest | * | / | % | ||||||||
Addition / Subtraction | + | – | |||||||||
Spread | << | >> | >>> | ||||||||
Relational | < | < = | > | > = | |||||||
Equality | = = | != | |||||||||
Bitwise AND | & | ||||||||||
Bitwise XOR | ^ | ||||||||||
Bitwise OR | | | ||||||||||
AND logic | && | ||||||||||
A logical OR | | | | ||||||||||
Other operators | ?: | New | Typeof | ||||||||
Assignment | = | + = | – = | * = | / = | >> = | << = | >>> = | & = | ^ = | | = |