JavaScript: Operators
This JavaScript tutorial explores the various operators available in the JavaScript language with syntax and examples.
Description
Operators are used in JavaScript code to perform comparisons, mathematical operations, and assignments. Let's take a look at the different types of operators.
Comparison Operators
Comparison operators are the operators that you would use to compare for equality, inequality as well as value (or data type) differences. Below is a list of the comparison operators in JavaScript:
Operator | Data Type | Description | Example |
---|---|---|---|
== | any | Tests for equality (performs implicit data type conversion) |
h == 15 |
!= | any | Tests for inequality (performs implicit data type conversion) |
h != 7 |
=== | any | Tests for equality and same data type | h === 3 |
!== | any | Tests for inequality or different data type | h !== 4 |
> | numbers, strings | Greater Than | h > 12 |
>= | numbers, strings | Greater Than or Equal | h >= 9 |
< | numbers, strings | Less Than | h < 5 |
<= | numbers, strings | Less Than or Equal | h <= 100 |
Mathematical Operators
Mathematical operators are the operators that you would use to perform math operations such as addition, subtraction, multiplication and division. Below is a list of the mathematical operators in JavaScript:
Operator | Data Type | Description | Example |
---|---|---|---|
+ | numbers | Addition | h = h + 8 |
- | numbers | Subtraction | h = h - 4 |
* | numbers | Multiplication | h = h * 12 |
/ | numbers | Division | h = h / 10 |
% | numbers | Remainder after division is performed | h = h % 7 |
++ | numbers | Increment (pre-increment or post-increment) |
++h h++ |
-- | numbers | Decrement (pre-decrement or post-decrement) |
--h h-- |
Assignment Operators
Assignment operators are the operators that you would use to assign a value to a variable. Below is a list of the assignment operators in JavaScript:
Operator | Data Type | Description | Example |
---|---|---|---|
= | any variable | Assign value | h = 8 |
+= | any variable | Add and assign value (same as h = h + 3) |
h += 3 |
-= | any variable | Subtract and assign value (same as h = h - 12) |
h -= 12 |
*= | any variable | Multiply and assign value (same as h = h * 7) |
h *= 7 |
/= | any variable | Divide and assign value (same as h = h / 5) |
h /= 5 |
%= | any variable | Divide and assign remainder value (same as h = h % 10) |
h %= 10 |
<<= | any variable | Left shift and assign value (same as h = h << 3) |
h <<= 3 |
>>= | any variable | Right shift with sign extension and assign value (same as h = h >> 9) |
h >>= 9 |
>>>== | any variable | Right shift with zero extension and assign value (same as h = h >>> 17) |
h >>>= 17 |
&= | any variable | Bitwise AND and assign value (same as h = h & 6) |
h &= 6 |
^= | any variable | Bitwise XOR and assign value (same as h = h ^ 4) |
h ^= 4 |
|= | any variable | Bitwise OR and assign value (same as h = h | 50) |
h |= 50 |
Logical Operators
Below is a list of the logical operators in JavaScript:
Operator | Data Type | Description | Example |
---|---|---|---|
&& | boolean | Logical AND | (h == 40 && j > 2) |
|| | boolean | Logical OR | (h == 35 || j < 10) |
! | boolean | Logical NOT (inverts the boolean value) |
!(h <= 7) |
String Operators
Below is a list of the string operators in JavaScript:
Operator | Data Type | Description | Example |
---|---|---|---|
+ | string | Concatenate | h = "Tech" + "OnTheNet" The variable h would contain the value "TechOnTheNet" |
+= | string | Concatenate by appending to the end of the string | h += "OnTheNet" If the variable h starts with a value of "Tech", the |
Bitwise Operators
Below is a list of the bitwise operators in JavaScript:
Operator | Data Type | Description | Example |
---|---|---|---|
& | integer |
Bitwise AND | h = h & 6 |
^ | integer (32 bit number) |
Bitwise XOR | h = h ^ 4 |
| | integer (32 bit number) |
Bitwise OR | h = h | 50 |
<< | integer (32 bit number) |
Left shift | h = h << 7 |
>> | integer (32 bit number) |
Right shift with sign extension | h = h >> 3 |
>>> | integer (32 bit number) |
Right shift with zero extension | h = h >>> 22 |
Conditional Operator
The conditional operator (known as the ?:
operator) is a special operator that allows you to assign one value when the condition is true and another value when the condition is false. This operator requires a little more explanation, so let's get started!
The syntax for this conditional operator is:
variable_name = (condition) ? true_value : false_value;
Parameters or Arguments
- variable_name
- The variable that will be assigned a value.
- condition
- The condition to be evaluated.
- true_value
- The value that is assigned to variable_name when the condition is true.
- false_value
- The value that is assigned to variable_name when the condition is false.
Example
Here is an example of how to use this conditional operator:
numType = (x < 0) ? "Negative" : "Positive";
In this example, if x is less than 0, the value "Negative" will be assigned to the variable numType. Otherwise, the value "Positive" will be assigned to numType.
Advertisements