Operators, special symbols (like =, +), allow programmers to create a result from one or more values. It is used to perform operations on operands such as E, m, and c in the following example.
Expressions rely on operators to evaluate a value. For
instance:
7 + 3;
As
the example above, the expression uses the + operator to add 7 and 3 operands
together and produces a value 10.
JavaScript has unary, binary, and ternary operators:
A unary operator requires only one operand, either before or after the operator.
For
example:
i++ (operand operator) – is called a postfix unary operator.
++i (operator operand) – is called a prefix unary operator.
A binary operator requires two operands, one operand before the
operator and another operand after the operator.
For example:
7 + 3 (operand1 operator operand2) – is called an infix binary operator.
A ternary operator requires three operands.
For example:
(num > 0) ? 'green' : 'red';
Here,
(num > 0) - operand1
? - operator1
'green' - operand2IfTrue
: - operator2
'red' - operand3IfFalse
There are different types of JavaScript operators:
For example: 3 + 5
// returns 8
Operator | Description | Example |
---|---|---|
+ | Addition – add numbers | 10 + 3 returns 13 |
- | Subtraction – subtracts numbers | 10 – 3 returns 7 |
* | Multiplication – multiplies numbers | 10 * 3 returns 30 |
/ | Division – divides numbers | 10 / 5 returns 2 |
% | Remainder – returns the division remainder | 10 % 3 returns 1 |
++ | Increment – increments numbers by 1 | i = 10; i++; returns 11 |
-- | Decrement – decrements numbers by 1 | i = 10; i--; returns 9 |
** | Exponentiation – raises the first operand to the power of the second operand |
2 ** 3 returns 8 |
Operator Precedence in JavaScript:
JavaScript
Operator precedence explains the
order of how to evaluate an arithmetic expression containing many operations.A list of the higher priorities are:
1.
Parentheses
(),
2.
Exponents
**,
3.
Multiplication * and Division /,
4.
Addition
+ and Subtraction -
Example:
10 + (23 + 4 - 1) * 5 - 5
= 10 + (27 - 1) * 5 - 5
= 10 +
26 * 5 - 5
= 10 + 130 - 5
= 140 - 5
= 135
In
the above example, Operations inside the parentheses are computed first and
operations with the same precedence such as + and – are computed from left to right.
For
example: i = 0 // The value of variable
i is 0
List of some Assignment
Operators:
Shorthand Operator | Name | Example & Meaning |
---|---|---|
+= | Addition Assignment | a += 5; // a = a + 5 |
-= | Subtraction Assignment | a -= 2; // a = a – 2 |
*= | Multiplication Assignment | a *= 3; // a = a * 3 |
/= | Division Assignment | a /= 2; // a = a / 2 |
%= | Remainder Assignment | a %= 2; // a = a % 2 |
**= | Exponentiation Assignment | a **= 2; // a = a**2 |
For
example: 'Satyo
' + 'Broto' // returns Satyo
Broto
10 + 10
returns 20 // adding two numbers will return the sum
10 + '10' returns
1010
// adding a number and a string will return a string
'ten' + 10 returns
ten10
// adding a string and a number will return a string
Operator | Description | Example & Result |
---|---|---|
== | Equal to: returns true if the operands are equal | 10 == 10 true 10 == ’10’ true 10 == 5 false |
!= | Not equal to: returns true if the operands are not equal | 10 != 10 false |
=== | Strict equal to: true if the operands are equal and of the same type |
10 === 10 true 10 === ‘10’ false |
!== | Strict not equal to: true if the operands are equal but of different type or not equal at all |
10 !== ‘10’ true |
> | Greater than: true if the left operand is greater than the right operand |
10 > 9 true |
>= | Greater than or equal to: true if the left operand is greater than or equal to the right operand |
10 >= 10 true |
< | Less than: true if the left operand is less than the right operand |
10 < 9 false |
<= | Less than or equal to: true if the left operand is less than or equal to the right operand |
10 <= 9 false |
● Logical Operators (&&,
||, !) are used to determine the logic between expressions or values and return
a Boolean value (either true or false). If operators are used with non-Boolean
values, they may return non-Boolean values.
(5
> 3) && (2 < 4) //
returns true
'Hello'
&& 'World!' // returns World!
For
non-Boolean values: The operator first converts a non-Boolean value to a
Boolean value. Boolean values that
are 0, null, undefined, NaN, and an empty string "" or '', become false. Other values become true
such as 1, 2, and a non-empty string (like '0',
'Broto', space string
" " or '
').
a | b | Return | |
---|---|---|---|
false | && | false | false |
false | && | true | false |
true | && | false | false |
true | && | true | true |
expression1 | expression2 | Return | |
---|---|---|---|
0 (false) | && | NaN (false) | 0 (false) |
0 (false) | && | 2 (true) | 0 (false) |
5 (true) | && | null (false) | null (false) |
10 (true) | && | 'Hello' (true) | Hello (true) |
In
the above example for non-Boolean values, &&
operator returns expression1 if it
can be converted to false;
otherwise, it returns expression2.
OR operator for Boolean values:
a | b | Return | |
---|---|---|---|
true | || | true | true |
true | || | false | true |
false | || | true | true |
false | || | false | false |
OR operator for Non-Boolean values:
expression1 | expression2 | Return | |
---|---|---|---|
10 (true) | || | 'Hello' (true) | 10 (true) |
5 (true) | || | null (false) | 5 (true) |
0 (false) | || | 2 (true) | 2 (true) |
0 (false) | || | NaN (false) | NaN (false) |
In
the above example for non-Boolean values, ||
operator returns expression1 if it
can be converted to true; otherwise,
it returns expression2.
Logical NOT ( ! ) operator returns true for false statements and false for true statement.
For
instance: !true => false;
console.log(3 == 3)
=> true
console.log(!3 ==
3) => false
● Conditional (Ternary) operator evaluates either of two expressions – a true or a false expression – based on a conditional expression. It is similar to an if…else statement.
let temp = 40;
const feverOrNot = temp >= 38 ? 'fever' : 'normal';
console.log(feverOrNot); //returns fever
Chain ternary operator is used as an alternative to an if…else if…else statement.
Chain ternary operator is used as an alternative to an if…else if…else statement.
Syntax:
conditionalExpression1 ? trueExpression1
: conditionalExpression2 ? trueExpression2
… …
…
: conditionalExpressionN ? trueExpressionN
: falseExpression
Example:
let temp = 44;
const feverOrNot = temp < 38 ? 'normal'
: temp < 40 ? 'fever'
: 'high fever'
console.log(feverOrNot); //returns high fever
● Nullish
coalescing
(??) operator returns the second expression when its first expression is null or undefined.
Syntax:
leftExpression ?? rightExpression
Example:
let leftExpre = undefined;
const nullishOperand = leftExpre ?? 'It is the right operand.';
console.log(nullishOperand) // returns It is the right operand.
let rightExpre = 'It is a right expression.';
const nullishOperand = leftExpre ?? rightExpre;
console.log(nullishOperand) //
returns It is a left expression.
Syntax: typeof operand
For
example:
console.log(typeof 3) //number
console.log(typeof 'Broto') //string
console.log(typeof true) //boolean
console.log(typeof null) //object
console.log(typeof [4,6,8]) //object
console.log(typeof undefined) //undefined
console.log(typeof function () {}) //function
0 Comments