Operatori de referința JavaScript
Sau
JavaScript Operators Reference
JavaScript Arithmetic Operators:
Arithmetic operators are used to perform arithmetic between variables and/or values.
Given that y = 5, the table below explains the arithmetic operators:
Operator Description Example Result in y Result in x
+ Addition x=y+2 Y=5 X=7
- Subtraction x=y-2 Y=5 X=3
* Multiplication x=y*2 Y=5 X=10
/ Division x=y/2 Y=5 X=2.5
% Modulus x=y%2 Y=5 X=1
(division
remainder)
++ Increment x = ++y Y=6 X=6
x = y++ Y=6 X=5
-- Decrement x = --y Y=4 X=4
x = y-- Y=4 X=5
JavaScript Assignment Operators
Assignment operators are used to assign values to JavaScript variables.
Given that x = 10 and y = 5, the table below explains the assignment operators:
Operator Example Same As Result in x
= X=y X= y X=5
+= X += y X=x+y X = 15
-= X -= y X=x-y X=5
*= X *= y X= x * y X = 50
/= X /= y X= x / y X=2
%= X %= y X=x%y X=0
Comparison Operators
Comparison operators are used in logical statements to determine equality or difference between variables or
values.
Given that x = 5, the table below explains the comparison operators:
Operator Description Comparing Returns
== equal to x == 8 False
x == 5 true
=== equal value and x === "5" False
equal type x === 5 True
!= not equal x != 8 True
!== not equal value or x !== "5" True
not equal type x !== 5 false
> Greater than x>8 False
< less than x<8 True
>= greater than or x >= 8 False
equal to
<= less than or equal to x <= 8 true
Conditional (Ternary) Operator
The conditional operator assigns a value to a variable based on a condition.
Syntax Example
variablename = (condition) ? voteable = (age < 18) ? "Too young":"Old
value1:value2 enough";
Example explained: If the variable "age" is a value below 18, the value of the variable
"voteable" will be "Too young", otherwise the value of voteable will be "Old enough".
Logical Operators
Logical operators are used to determine the logic between variables or values.
Given that x = 6 and y = 3, the table below explains the logical operators:
Operator Description Example
&& And (x < 10 && y > 1) is true
|| Or (x === 5 || y === 5) is
false
! not !(x === y) is true
JavaScript Bitwise Operators
Bit operators work on 32 bits numbers. Any numeric operand in the operation is
converted into a 32-bit number. The result is converted back to a JavaScript number.
Operator Description Example Same as Result Decimal
& AND X=5&1 0101 & 0001 0001 1
| OR x=5|1 0101 | 0001 0101 5
~ NOT x=~5 ~0101 1010 10
^ XOR X=5^1 0101 ^ 0001 0100 4
<< Left shift X = 5 << 1 0101 << 1 1010 10
>> Right shift X = 5 >> 1 0101 >> 1 0010 2
The examples above uses 4 bits unsigned examples. But JavaScript uses 32-bit signed numbers.
Because of this, in JavaScript, ~ 5 will not return 10. It will return -6.
~00000000000000000000000000000101 will return 11111111111111111111111111111010