JavaScript operator
JavaScript operators are used to assign values, compare values, perform arithmetic operations, and so on.
JavaScript arithmetic operator
Arithmetic operators are used to perform operations on two variables or values.
Assignment y is 5, and the following table will show you the use of arithmetic operators:
Operator | Describe | Example | y value | The x value | Online instance |
---|---|---|---|---|---|
+ | 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 . . . |
++ | Self-increase | x = ++y | y = 6 | x = 6 | Examples . . . |
x = y++ | y = 6 | x = 5 | Examples . . . | ||
-- | Self-reducing | x = --y | y = 4 | x = 4 | Examples . . . |
x = y-- | y = 4 | x = 5 | Examples . . . |
For arithmetic operators, you can read our JavaScript operator tutorial.
JavaScript assignment operator
The assignment operator is used to assign values to JavaScript variables.
Given the x-10 and y-5, the following table explains the assignment operators:
Operator | Example | Same As | The x value | Online instance |
---|---|---|---|---|
= | 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 . . . |
For assignment operators, you can read our JavaScript operator tutorial.
JavaScript string operator
The operator, the operator, can be used to connect strings.
Given that text1 is "Good", text2 is "Morning", and text3 is "", the following table explains the use of string operators:
Operator | Example | text1 | text2 | text3 | Online instance |
---|---|---|---|---|---|
+ | text3 = text1 + text2 | "Good " | "Morning" | "Good Morning" | Examples . . . |
+= | text1 += text2 | "Good Morning" | "Morning" | "" | Examples . . . |
Comparison operator
Comparison operators are used to determine the judgment of logical statements to determine whether two values or variables are equal.
Given the x-5, the following table shows the use of comparison operators:
Operator | Describe | Comparison | Results | Online instance |
---|---|---|---|---|
== | Equals | x == 8 | false | Examples . . . |
x == 5 | true | Examples . . . | ||
=== | Values and types are equal (constant equal) | x === "5" | false | Examples . . . |
x === 5 | true | Examples . . . | ||
!= | Not equal to | x != 8 | true | Examples . . . |
!== | Values and types are equal (insemost equal to) | x !== "5" | true | Examples . . . |
x !== 5 | false | Examples . . . | ||
> | Greater than | x > 8 | false | Examples . . . |
< | Less than | x < 8 | true | Examples . . . |
>= | Is greater than or equal to | x >= 8 | false | Examples . . . |
<= | Less than or equal to | x <= 8 | true | Examples . . . |
For comparison operators, you can read our JavaScript comparison operator tutorial.
Conditional operator
Conditional operators are used for condition-based assignment operations.
Given the x-6 and y-3, the following performance shows the operation of the conditional operator:
Grammar | Example | Online instance |
---|---|---|
Variables - ( conditions ) ? Value 1: Value 2 | voteable = (age & 18) ? "Too young" : "Old enough" | Examples . . . |
The logical operator
Logical operators are used to determine the logical relationship between variables or values.
Given the x-6 and y-3, the following example demonstrates the use of logical operators:
Operator | Describe | Example |
---|---|---|
&& | And | (x slt; 10 and y and 1) is true |
|| | Or | (x s 5 || y s 5) is false |
! | Non - | ! (x s y) is true |
JavaScript bit operator
Bit operators work on 32-bit numbers. A ny digital operation will be converted to 32 bits. T he result is converted to a JavaScript number.
Operator | Describe | Example | Similar to | Results | Decimal |
---|---|---|---|---|---|
& | AND | x = 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | OR | x = 5 | 1 | 0101 | 0001 | 0101 | 5 |
~ | Take the opposite | x = ~ 5 | ~0101 | 1010 | 10 |
^ | different or not | x = 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
<< | Left/td> | x = 5 << 1 | 0101 << 1 | 1010 | 10 |
>> | Move to the right | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 |