JavaScript
Operators
Contents
1. Overview
2. Assignment Operators
3. Arithmetic Operators
4. Comparison Operator
5. Logical Operators
6. String Operator
7. Ternary Operator
8. Nullish coalescing assignment (??=)
9. Nullish coalescing operator (??)
2
1. Overview
• In computer programming, an operator is a character that represents a
specific mathematical or logical action or process.
• An operator is capable of manipulating a certain value or operand.
• Example:
· 2 + 3, here + is an operator that performs addition, and 2 and 3 are
operands.
• JavaScript Operator
1. Assignment Types:
Operator
5. Ternary
2. Arithmetic Operator
6. String Operators
3. Comparison Operator
7. Other Operators
4. Logical Operator
3
2. Assignment Operators
• Assignment operators are used to assign values to variables.
• Example:
let x = 5 (the = operator is used to assign value 5 to variable x)
• List of assignment operators:
= AssignmentOperator
+= Addition Assignment
-= Subtraction Assignment
*= Multiplication Assignment
/= Division Assignment
%= Remainder Assignment
**= Exponentiation Assignment 4
3. Arithmetic Operators
• Arithmetic operators are used to perform arithmetic calculations.
• Example:
let number = 3 + 5
• List of assignment operators:
+ Addition ++ Increment by 1
- Subtraction -- Decrement by 1
* Multiplication ** Exponentiation
/ Division
% Remainder
5
4. Comparison Operators
• Comparison operators compares 2 values and return a Boolean value
(true/false).
• Example:
let a = 3, b = 2
console.log(a > b) // true
• List of comparison operators:
== Equal to >= Greater than or
equal to
!= Not equal to < Less than
=== Strict equal to <= Less than or equal to
!== Strict not equal to
> Greater than 6
5. Logical Operators
• Logical operators perform logical operations and return a Boolean value
(true/false).
• Example:
const x = 5, y = 3
(x < 6) && (y < 5) // true
• List of logical operators:
&& Logical AND
|| Logical OR
! Logical NOT
7
6. String Operators
• In JS, you can also use the + operator to concatenate (join) two or more
strings.
• Example:
console.log(‘hello’ + ‘world’)
Let a = ‘JavaScript’
a += ‘ tutorial’ // a = a + ‘ tutorial’
console.log(a)
• Output
helloworld
JavaScript tutorial 8
7. Ternary Operators
• A ternary operator evaluates a condition and executes a block of code on
condition.
• Syntax:
condition ? expression_1 : expression_2
· If the condition is true, expression_1 is executed
· If the condition is false, expression_2 is executed
• Example:
let result = (score >= 50) ? ‘Pass’ : ‘Fail’
console.log(`You ${result} the exam.`)
// You pass the exam.
9
7. Ternary Operators (cont.)
• Example of nested ternary operators:
let a = 3
let result =
(a >= 0) ? (a == 0) ? ‘Zero’: ‘Positive’ : ‘Negative’
console.log(‘The number is ${result}.’)
// The number is Positive.
• Note:
• You should try to avoid nested ternary operators whenever possible as
they make your code hard to read.
10
8. Nullish coalescing assignment (??=)
The nullish coalescing assignment (x ??= y) operator, also known as
the logical nullish assignment operator, only assigns
if x is nullish (null or undefined).
const a = { duration: 50 };
a.duration ??= 10;
console.log(a.duration); // Expected output: 50
a.speed ??= 25;
console.log(a.speed); // Expected output: 25
11
9. Nullish coalescing operator (??)
The nullish coalescing (??) operator is a logical operator that returns its right-
hand side operand when its left-hand side operand is null or undefined, and
otherwise returns its left-hand side operand.
const foo = null ?? 'default string';
console.log(foo); // Expected output: "default string"
const baz = 0 ?? 42;
console.log(baz); // Expected output: 0
12
Thank you
Perfect Practice, Does Perfect Thing
13