JavaScript Tutorial || JavaScript Operators
JavaScript Operators
Operators are symbols or keywords that tell the JavaScript engine to perform some sort of actions.For example, 5 + 2 is equal to 7. Here 5 and 2 are called operands and ‘+’ is called the operator. JavaScript supports the following types of operators.
example
var x = 25;var y = 35;var z = "25"; alert(x == z); // Outputs: truealert(x === z); // Outputs: falsealert(x != y); // Outputs: true
Operators are symbols or keywords that tell the JavaScript engine to perform some sort of actions.For example, 5 + 2 is equal to 7. Here 5 and 2 are called operands and ‘+’ is called the operator. JavaScript supports the following types of operators.
- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators
Arithmetic Operators
The arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | $x + $y | Sum of $x and $y |
- | Subtraction | $x - $y | Difference of $x and $y. |
* | Multiplication | $x * $y | Product of $x and $y. |
/ | Division | $x / $y | Quotient of $x and $y |
% | Modulus | $x % $y | Remainder of $x divided by $y |
example
var x = 6;var y = 4;(x + y)= 10
Assignment Operators
The assignment operators are used to assign values to variables.
example
var x; x = 10; x= 10x = 20;x += 30; x=50
String Operators
There are two operators which can also used be for strings.
example
var str1 = "Hello";var str2 = " World!";str1 + str2 = Hello World!
Incrementing and Decrementing Operators
The increment/decrement operators are used to increment/decrement a variable's value.
example
var x; x = 10;alert(++x); // Outputs: 11alert(x); // Outputs: 11
Logical Operators
The logical operators are typically used to combine conditional statements.
example
var age = 20;
if((age == 20) || ((age != 0))){ alert("done");} else{ alert("false");}
Comparison Operators
The comparison operators are used to compare two values in a Boolean fashion.
The assignment operators are used to assign values to variables.
Operator | Description | Example | Is The Same As |
---|---|---|---|
= | Assign | x = y | x = y |
+= | Add and assign | x += $ | x = x + y |
-= | Subtract and assign | x -= y | x = x - y |
*= | Multiply and assign | x *= y | x = x * y |
/= | Divide and assign quotient | x /= y | x = x / y |
%= | Divide and assign modulus | x %= y | x = x % y |
example
var x; x = 10; x= 10x = 20;x += 30; x=50
String Operators
There are two operators which can also used be for strings.
Operator | Description | Example | Result |
---|---|---|---|
+ | Concatenation | str1 + str2 | Concatenation of str1 and str2 |
+= | Concatenation assignment | str1 += str2 | Appends the str2 to the str1 |
example
var str1 = "Hello";var str2 = " World!";str1 + str2 = Hello World!
Incrementing and Decrementing Operators
The increment/decrement operators are used to increment/decrement a variable's value.
Operator | Name | Effect |
---|---|---|
++x | Pre-increment | Increments x by one, then returns x |
x++ | Post-increment | Returns x, then increments x by one |
--x | Pre-decrement | Decrements x by one, then returns x |
x-- | Post-decrement | Returns x, then decrements x by one |
example
var x; x = 10;alert(++x); // Outputs: 11alert(x); // Outputs: 11
Logical Operators
The logical operators are typically used to combine conditional statements.
Operator | Name | Example | Result |
---|---|---|---|
&& | And | x && y | True if both x and y are true |
|| | Or | x || y | True if either x or y is true |
! | Not | !x | True if x is not true |
example
var age = 20;
if((age == 20) || ((age != 0))){ alert("done");} else{ alert("false");}
Comparison Operators
The comparison operators are used to compare two values in a Boolean fashion.
Operator | Name | Example | Result |
---|---|---|---|
== | Equal | x == y | True if x is equal to y |
=== | Identical | x===y | True if x is equal to y, and they are of the same type |
!= | Not equal | x != y | True if x is not equal to y |
!== | Not identical | x!==y | True if x is not equal to y, or they are not of the same type |
< | Less than | x < y | True if x is less than y |
> | Greater than | x > y | True if x is greater than y |
>= | Greater than or equal to | x >= y | True if x is greater than or equal to y |
<= | Less than or equal to | x <= y | True if x is less than or equal to y |
example
var x = 25;var y = 35;var z = "25"; alert(x == z); // Outputs: truealert(x === z); // Outputs: falsealert(x != y); // Outputs: true