Operators are the symbols that actually operate the logic. In JavaScript we have some interesting operators as given below:
=
is used as basic assignment operator in JavaScript. A value at the right side is assigned to the variable on the left side. It is used for both string and numeric values.
Example:
var a = 7; will assign the value 7 to variable a
var b = 5; will assign the value 5 to b
var c = a + b ; will assign the value of (a+b) i.e 7 to variable c
var str = “NIELIT”; will assign string “NIELIT” to variable str
JavaScript supports the following Arithmetic operators:
Operator | Purpose | Example | Output (a = ?) |
---|---|---|---|
+ | Addition | a = 5 + 2; | 7 |
- | Subtraction | a = 5 - 2; | 3 |
* | Multiplication | a = 5 * 2; | 10 |
/ | Division (for quotient) | a = 5 / 2; | 2 (quotient) |
% | Division (for remainder) | a = 5 % 2; | 1 (remainder) |
++ | Increment | a = 5 ; a++; | 6 |
-- | Decrement | a = 5 ; a--; | 4 |
** | Exponentiation | a = 5 ** 2; | 25 |
Apart from basic assignment operator “=”, JavaScript also support combination of Assignment and Arithmetic operator as supported by several other languages. This eventually reduces the lines of code:
Operator | Purpose | Example | Similar to: |
---|---|---|---|
+= | Addition assignment | a += b | a = a + b |
-= | Subtraction assignment | a -= b | a = a - b |
*= | Multiplication assignment | a *= b | a = a * b |
/= | Division (for quotient) assignment | a /= b | a = a / b |
%= | Division (for remainder) assignment | a %= b | a = a % b |
**= | Exponentiation assignment | a **= b | a = a ** b |
JavaScript supports standard comparison operators as given below:
Operator | Purpose | Example | Output (a = ?) |
---|---|---|---|
== | To check equality of value | a = 5 == 2; | false |
=== | To check equality of value and type | a = 5 === "five"; | false |
!= | To check inequality of value | a = 5 != 2; | true |
!== | To check inequality of value and type | a = 5 !=== "five"; | true |
> | To check if greater | a = 5 > 2; | true |
< | To check if smaller | a = 5 < 2; | false |
>= | To check if greater or equal | a = 5 >= 2; | true |
<= | To check if smaller or equal | a = 5 <= 2; | false |
JavaScript provides three Logical operators:
Operator | Purpose | Example | Output (a = ?) |
---|---|---|---|
&& | To combine condition in logical AND | a = (5>2) && (3<1); | false |
|| | To combine condition in logical OR | a = (5>2) || (3<1); | true |
! | To negate the condition | a = !(5>2); | false |
=
The + and += operator may also be used be used to add or concatenate strings. It may also be used with a combination of strings and numbers and in this case output will be a string.
Example 1:
var str1 = "NIELIT";
var str2 = "DELHI";
var str3 = str1 + " " + str2;
The output value of str3 will be:
“NIELIT DELHI”
Example 2:
var a = 5 + 6; //11 and is number
var b = "5" + 6; //56 and is a string
var c = "Good" + 9; //Good9 and is a string
The ternary operator assigns a value to a variable based on the condition.
Syntax:
variablename = (condition) ? value1:value2
Example:
IsMinor = (age < 18) ? "Yes":"No";
Here in this example, if the value of variable a is less than 18, the string “Yes” is assigned to the variable IsMinor otherwise “No”.