Cross_Column

Saturday, 31 January 2026

Operators in JavaScript, Conditional Statements, Loops



JavaScript Basics Tutorial: Operators, Conditional Statements, and Loops

This tutorial covers three fundamental concepts of JavaScript programming:

  • Operators in JavaScript
  • Conditional Statements
  • Loops

These concepts are the foundation of logic building and real-world application development in JavaScript.


🔹 Operators in JavaScript

Operators are used to perform operations on variables and values.

1️⃣ Arithmetic Operators

let a = 10;
let b = 5;

console.log(a + b);  // 15
console.log(a - b);  // 5
console.log(a * b);  // 50
console.log(a / b);  // 2
console.log(a % b);  // 0 

2️⃣ Assignment Operators

let x = 10;
x += 5;   // x = x + 5
x -= 2;   // x = x - 2
x *= 2;   // x = x * 2
x /= 2;   // x = x / 2

3️⃣ Comparison Operators

let p = 10;
let q = "10";

console.log(p == q);   // true
console.log(p === q);  // false
console.log(p != q);   // false
console.log(p !== q);  // true
console.log(p > 5);    // true 

4️⃣ Logical Operators

let age = 20;

console.log(age > 18 && age < 60); // true
console.log(age > 18 || age < 10); // true
console.log(!(age > 18));          // false 

🔹 Conditional Statements in JavaScript

Conditional statements are used to make decisions in programs.

1️⃣ if Statement

let age = 20;

if (age >= 18) {
console.log("You are eligible to vote");
} 

2️⃣ if...else Statement

let marks = 45;

if (marks >= 50) {
console.log("Pass");
} else {
console.log("Fail");
} 

3️⃣ if...else if...else

let score = 85;

if (score >= 90) {
console.log("Grade A");
} else if (score >= 70) {
console.log("Grade B");
} else {
console.log("Grade C");
} 

4️⃣ switch Statement

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");
} 

🔹 Loops in JavaScript

Loops are used to execute a block of code multiple times.

1️⃣ for Loop

for (let i = 1; i <= 5; i++) {
    console.log(i);
}

2️⃣ while Loop

let i = 1;

while (i <= 5) {
console.log(i);
i++;
} 

3️⃣ do...while Loop

let i = 1;

do {
console.log(i);
i++;
} while (i <= 5); 

4️⃣ for...of Loop (Arrays)

let fruits = ["Apple", "Banana", "Mango"];

for (let fruit of fruits) {
console.log(fruit);
} 

5️⃣ for...in Loop (Objects)

let student = {
    name: "Rahul",
    age: 20,
    city: "Delhi"
};

for (let key in student) {
console.log(key + " : " + student[key]);
} 

🧠 Real-World Example

let users = [18, 25, 12, 40];

for (let age of users) {
if (age >= 18) {
console.log(age + " is Eligible");
} else {
console.log(age + " is Not Eligible");
}
} 

❓ Interview Questions

  • Difference between == and === in JavaScript?
  • What are logical operators?
  • Difference between for and while loop?
  • What is switch case?
  • Difference between for...in and for...of?

✅ Conclusion

Operators, conditional statements, and loops are the building blocks of JavaScript programming. Mastering these concepts helps you write logical, dynamic, and powerful applications.

These fundamentals are essential for frontend development, backend development, and automation scripting.


🚀 Happy Learning JavaScript!

Follow this blog for more JavaScript, Python, Automation, and Web Development tutorials.

No comments:

Post a Comment

Few More

this keyword in JavaScript

this Keyword in JavaScript – Complete Beginner to Advanced Guide The this keyword in JavaScript is one of the most confusing co...