JavaScript Operators
JavaScript assignment operator is used to compare values, perform arithmetic operations and the like.
JavaScript Arithmetic Operators
Arithmetic operators are used to perform an operation on two variables or values.
Assignmenty = 5, the following table will show that you use the arithmetic operators:
Operators | description | example | y value | x value | Online examples |
---|---|---|---|---|---|
+ | addition | x = y + 2 | y = 5 | x = 7 | Examples >> |
- | Subtraction | x = y - 2 | y = 5 | x = 3 | Examples >> |
* | multiplication | x = y * 2 | y = 5 | x = 10 | Examples >> |
/ | division | x = y / 2 | y = 5 | x = 2.5 | Examples >> |
% | remainder | x = y% 2 | y = 5 | x = 1 | Examples >> |
++ | Increment | x = ++ y | y = 6 | x = 6 | Examples >> |
x = y ++ | y = 6 | x = 5 | Examples >> | ||
- | Decrement | x = --y | y = 4 | x = 4 | Examples >> |
x = y-- | y = 4 | x = 5 | Examples >> |
About arithmetic operators, you can read our JavaScript tutorial operator .
JavaScript Assignment Operators
JavaScript assignment operator is used to assign values to variables.
Given x = 10 and y = 5, the table below explains the assignment operator:
Operators | example | Same As | x value | Online examples |
---|---|---|---|---|
= | x = y | x = y | x = 5 | Examples >> |
+ = | x + = y | x = x + y | x = 15 | Examples >> |
- = | x - = y | x = x - y | x = 5 | Examples >> |
* = | x * = y | x = x * y | x = 50 | Examples >> |
/ = | x / = y | x = x / y | x = 2 | Examples >> |
% = | x% = y | x = x% y | x = 0 | Examples >> |
About assignment operator, you can read our JavaScript tutorial operator .
JavaScript String Operators
+ Operator + = operator can be used to concatenate strings.
Giventext1 = "Good", text2 ="Morning", and text3 = "", the following table explains the use of string operators:
Operators | example | text1 | text2 | text3 | Online examples |
---|---|---|---|---|---|
+ | text3 = text1 + text2 | "Good" | "Morning" | "Good Morning" | Examples >> |
+ = | text1 + = text2 | "Good Morning" | "Morning" | ' " | Examples >> |
Comparison
Analyzing comparison logic for the statement to determine given two values or variables are equal.
Givenx = 5, the following table shows the comparison of the use of:
Operators | description | Compare | result | Online examples |
---|---|---|---|---|
== | equal | x == 8 | false | Examples >> |
x == 5 | true | Examples >> | ||
=== | Values, and types are equal (always equals) | x === "5" | false | Examples >> |
x === 5 | true | Examples >> | ||
! = | not equal to | x! = 8 | true | Examples >> |
! == | Value and type are not, etc. (not always equals) | x! == "5" | true | Examples >> |
x! == 5 | false | Examples >> | ||
> | more than the | x> 8 | false | Examples >> |
< | Less than | x <8 | true | Examples >> |
> = | greater than or equal to | x> = 8 | false | Examples >> |
<= | less than or equal to | x <= 8 | true | Examples >> |
About comparison, you can read our JavaScript tutorial comparison .
Conditional operator
The conditional operator based on the conditions for the assignment operator.
Givenx = 6 and y = 3, the following table demonstrates the conditional operator of operations:
grammar | example | Online examples |
---|---|---|
Variable = (condition) Value 1:? 2 value | ? Voteable = (age & 18) "Too young": "Old enough" | Examples >> |
Logical Operators
Logical operator to determine the logical relationships between variables or values.
Givenx = 6 and y = 3, the following example demonstrates the use of logical operators:
Operators | description | example |
---|---|---|
&& | and | (X <10 && y> 1) is true |
|| | or | (X == 5 || y == 5) is false |
! | non- | ! (X == y) is true |
JavaScript Bitwise Operators
Bitwise operators work on 32-bit numbers. Any digital operations will be converted to 32-bit. The results will be converted to a JavaScript number.
Operators | description | example | Like | result | Decimal |
---|---|---|---|---|---|
& | AND | x = 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | OR | x = 5 | 1 | 0101 | 0001 | 0101 | 5 |
~ | Negate | x = ~ 5 | ~ 0101 | 1010 | 10 |
^ | or | x = 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
<< | Left / td> | x = 5 << 1 | 0101 << 1 | 1010 | 10 |
>> | Right | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 |