TGViewer
Html and Css & Jsآموزش Html and Css & Jsآموزش @htmlcss_channels · 23.1K subscribers
Post #2417 1.3K
🚀 JavaScript Interview Questions with Answers — Part 4

31. What is if/else and switch?

Both are conditional statements used to make decisions in JavaScript.

if/else 
Executes code based on conditions. 

let age = 18;

if (age >= 18) {
    console.log("Adult");
} else {
    console.log("Minor");
}


switch 
Used when checking multiple possible values. 

let day = 2;

switch(day) {
    case 1:
        console.log("Monday");
        break;

    case 2:
        console.log("Tuesday");
        break;

    default:
        console.log("Invalid Day");
}


Difference: 
if/else - Better for conditions/ranges, Flexible 
switch - Better for exact values, Cleaner for many cases

32. What is the difference between for, for...in, and for...of?

for 
Traditional loop. 

for (let i = 0; i < 3; i++) {
    console.log(i);
}


for...in 
Used for iterating object keys. 

const person = {
    name: "Deepak",
    age: 25
};

for (let key in person) {
    console.log(key);
}


for...of 
Used for iterable values like arrays. 

const nums = [1, 2, 3];

for (let num of nums) {
    console.log(num);
}


Key Difference: 
Loop - Best For 
for - Full control 
for...in - Object properties 
for...of - Array values

33. What is the while and do-while loop?

Both loops execute code repeatedly while a condition is true.

while Loop 
Condition checked before execution. 

let i = 1;

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


do-while Loop 
Runs at least once before checking condition. 

let i = 1;

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


Difference: 
while - Condition first, May run zero times 
do-while - Code first, Runs at least once

34. What is the ternary operator?

The ternary operator is a shorthand for if/else.

Syntax: 
condition? trueValue : falseValue

Example: 

let age = 20;

let result = age >= 18 ? "Adult" : "Minor";

console.log(result);


Benefits: 
• Shorter code
• Cleaner simple conditions

35. What is short-circuit evaluation?

JavaScript stops evaluating expressions as soon as the result is known.

Using && 
Returns first falsy value. 

console.log(false && "Hello");


Output: 
false

Using || 
Returns first truthy value. 

console.log("" || "Default");


Output: 
Default

Practical Example: 

let username = "";

let displayName = username || "Guest";

console.log(displayName);


36. What is the difference between break and continue?

Keyword - Purpose 
break - Stops the loop completely 
continue - Skips current iteration

break Example 

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


Output: 
1 
2

continue Example 

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


Output: 
1 
2 
4 
5

37. How do you iterate over an array or object?

Array Iteration 
Using forEach() 

const nums = [1, 2, 3];

nums.forEach(num => {
    console.log(num);
});


Object Iteration 
Using for...in 

const person = {
    name: "Deepak",
    age: 25
};

for (let key in person) {
    console.log(key, person[key]);
}


Using Object.keys() 

Object.keys(person).forEach(key => {
    console.log(key);
});


38. How do you implement recursion?

Recursion is when a function calls itself until a stopping condition is met.

Example: Factorial

function factorial(n) {
    if (n === 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

console.log(factorial(5));


Output: 
120

Important Parts: 
1. Base condition
2. Recursive call

Without a base condition → infinite recursion.
  • ❤ 1
  • 👍 1
More from @htmlcss_channels
  1. Sep 28, 2026؛CSS Clamp — اندازه‌گذاری هوشمند فرمول ساده ؛clamp(کمترین, مقدار_ایده‌آل, بیشترین) مثل یه…
  2. Sep 28, 2026🔅 CSS Clamp clamp(minimum, preferred, maximum); In this example, the preferred value is 5…
  3. Sep 27, 2026🚀 نقشه راه ورود به بازار فریلنسینگ بین‌المللی طراح، برنامه‌نویس، دیزاینر یا تولیدکننده مح…
  4. Sep 26, 2026برای معرفی پروژه هاتون به کارفرما بهتره از این روش استفاده کنید این وبسایت پروژه هاتون رو…
  5. Sep 24, 2026۵ منبع رایگان کاربردی برای طراحان و توسعه‌دهندگان وبسایت! اگه دنبال کامپوننت‌های آماده، آی…
  6. Sep 23, 2026HTML: Responsive images 🔅 تصاویر واکنش‌گرا در HTML با اين تکنیک، مرورگر خودش بهترین نسخه…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →