Operators
An operator is a symbol or keyword that tells JavaScript to perform a specific operation on one or more values (called operands).
For example, in the expression:
let sum = 10 + 5;//10 and 5 are operands.+ is the operator.The result is 15.
Types of Operators
JavaScript provides several types of operators:
1. Arithmetic Operators
Arithmetic operators perform mathematical calculations.
- Addition 10 + 5
- Subtraction 10 - 5
- Multiplication 10 * 5 / Division 10 / 5 % Modulus (Remainder) 10 % 3 ** Exponentiation 2 ** 3
2. Assignment Operators
Assignment operators assign values to variables.
= x = 5 Assign
+= x += 2 Add and assign
-= x -= 2 Subtract and assign
*= x *= 2 Multiply and assign
/= x /= 2 Divide and assign
3. Comparison Operators
Comparison operators compare two values and return true or false.
== Equal to
!= Not equal
Greater than
< Less than
= Greater than or equal
<= Less than or equal
4. Logical Operators
Logical operators combine multiple conditions.
&& AND
|| OR
! NOT
5. Increment & Decrement Operators
These operators increase or decrease a value by 1.
Increment (++)
let count = 5;
count++;
console.log(count);
Output
6
Decrement (--)
let count = 5;
count--;
console.log(count);
Output
4
6. Ternary Operator
The ternary operator is a shorter way to write an if...else statement.
Syntax
condition ? value1 : value2;
Example
let age = 20;
let result = age >= 18 ? "Adult" : "Minor";
console.log(result);
Output
Adult
Reference:
JavaScript Operators-Geeksforgeeks
CONDITIONAL STATEMENTS
A conditional statement is used to execute different blocks of code depending on whether a condition is true or false.
Types of Conditional Statements
JavaScript provides the following conditional statements:
1. if Statement
The if statement executes a block of code only if the condition is true.
Syntax
if (condition) {
// Code to execute
}
Example
let age = 20;
if (age >= 18) {
console.log("You are eligible to vote.");
}
Output
You are eligible to vote.
2. if...else Statement
The if...else statement executes one block if the condition is true, otherwise it executes another block.
Syntax
if (condition) {
// Executes if true
} else {
// Executes if false
}
Example
let age = 16;
if (age >= 18) {
console.log("Eligible to vote");
} else {
console.log("Not eligible to vote");
}
Output
Not eligible to vote
3. if...else if...else Statement
Use this when you need to check multiple conditions.
Syntax
if (condition1) {
} else if (condition2) {
} else {
}
Example
let marks = 85;
if (marks >= 90) {
console.log("Grade A");
}
else if (marks >= 75) {
console.log("Grade B");
}
else if (marks >= 50) {
console.log("Grade C");
}
else {
console.log("Fail");
}
Output
Grade B
4. Nested if
A nested if means placing one if statement inside another.
Example
let age = 20;
let hasLicense = true;
if (age >= 18) {
if (hasLicense) {
console.log("You can drive.");
}
}
Output
You can drive.
5. switch Statement
The switch statement is used when you want to compare one value against many possible values.It is often cleaner than writing many else if statements.
Syntax
switch(expression){
case value1:
// Code
break;
case value2:
// Code
break;
default:
// Code
}
Example
let day = 3;
switch(day){
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid Day");
}
Output
Wednesday
Top comments (0)