JavaScript operators are employed for a variety of mathematical and logical computations.
The assignment operator (=) is used to assign a value to a variable.
| let x = 10; |
|
let x = 10; // Assign the value 5 to x |
JavaScript Addition
The Addition Operator (+) adds numbers:
| let x = 5; let y = 2; let z = x + y; |
The multiplication operator (*) performs multiplication on numbers.
| let x = 5; let y = 2; let z = x * y; |
JavaScript encompasses various operator types:
Arithmetic Operators facilitate numerical calculations.
| let a = 3; let x = (100 + 50) * a; |
|
Operator |
Description |
|
+ |
Addition |
|
– |
Subtraction |
|
* |
Multiplication |
|
** |
Exponentiation (ES2016) |
|
/ |
Division |
|
% |
Modulus (Division Remainder) |
|
++ |
Increment |
|
— |
Decrement |
Assignment operators in JavaScript are responsible for assigning values to variables.
For example, the Addition Assignment Operator (+=) adds a value to a variable.
| let x = 10; x += 5; |
|
Operator |
Example |
Same As |
|
= |
x = y |
x = y |
|
+= |
x += y |
x = x + y |
|
-= |
x -= y |
x = x – y |
|
*= |
x *= y |
x = x * y |
|
/= |
x /= y |
x = x / y |
|
%= |
x %= y |
x = x % y |
|
**= |
x **= y |
x = x ** y |
|
Operator |
Description |
|
== |
equal to |
|
=== |
equal value and equal type |
|
!= |
not equal |
|
!== |
not equal value or not equal type |
|
> |
greater than |
|
< |
less than |
|
>= |
greater than or equal to |
|
<= |
less than or equal to |
|
? |
ternary operator |
Each of the comparison operators listed can also be applied to strings.
Example
| let text1 = “A”; let text2 = “B”; let result = text1 < text2; |
It’s important to note that when comparing strings, they are compared alphabetically.
Example
|
let text1 = “20”; |
JavaScript String Addition
The plus symbol (+) can also concatenate strings, in addition to adding numerical values.
Example
| let text1 = “John”; let text2 = “Doe”; let text3 = text1 + ” “ + text2; |
The += assignment operator can also be employed for string concatenation.
Example
| let text1 = “What a very “; text1 += “nice day”; |
The result of text1 will be:
What a very nice day |
The + operator is referred to as the concatenation operator when applied to strings.
When you add two numbers together, the result is their sum, but when you add a number and a string, the result is a string.
|
let x = 5 + 5; |
The outcome of x, y, and z will be:
| 10 55 Hello5 |
Note:When you combine a number and a string, the outcome will be a string! |
|
Operator |
Description |
|
&& |
logical and |
|
|| |
logical or |
|
! |
logical not |
|
Operator |
Description |
|
typeof |
Provides the data type of a variable. |
|
instanceof |
Returns true if an object belongs to a particular object type. |