Loops & Iteration plus Conditional Logic Flashcards

(32 cards)

1
Q

What is a conditional statement?

A

A statement that executes code only if a specific condition is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What keyword is used for a basic condition?

A

The if keyword. Example: if (x > 10) { console.log('big'); }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you add an alternative condition?

A

Use else if. Example: if (x > 10) {...} else if (x > 5) {...}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you provide a fallback for when no conditions are true?

A

Use else. Example: if (...) {...} else {...}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the ternary operator?

A

A shorthand for if...else. Example: let msg = age >= 18 ? 'Adult' : 'Minor';

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you compare values strictly?

A

Use === and !== for strict equality and inequality (compares type and value).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between == and ===?

A

== compares values after type conversion; === compares both type and value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you check multiple conditions?

A

Use logical operators && (AND), || (OR), and ! (NOT). Example: if (x > 0 && y > 0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you use a switch statement?

A

Use switch(expression) with case labels. Example: switch(day){ case 'Mon': ... break; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why is break used in a switch statement?

A

To prevent fall-through (stops execution of the next case).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does default do in a switch statement?

A

It defines the code to run if no case matches.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is short-circuit evaluation?

A

When JavaScript stops evaluating as soon as the result is known. Example: true || false returns true immediately.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a loop?

A

A control structure that repeats code until a condition is false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a for loop used for?

A

To run code a specific number of times. Example: for (let i = 0; i < 5; i++) {...}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the three parts of a for loop?

A

Initialization, condition, and increment. Example: for (let i=0; i<10; i++)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a while loop?

A

Runs code while a condition is true. Example: while (count < 5) {...}

17
Q

What is a do...while loop?

A

Executes code at least once before checking the condition. Example: do {...} while (x < 5);

18
Q

What does the break statement do in loops?

A

Immediately exits the loop.

19
Q

What does the continue statement do?

A

Skips the current iteration and moves to the next one.

20
Q

How do you loop through an array?

A

Use a for loop or .forEach(). Example: arr.forEach(item => console.log(item));

21
Q

How do you loop through object properties?

A

Use a for...in loop. Example: for (let key in obj) {...}

22
Q

How do you loop through iterable objects like arrays?

A

Use a for...of loop. Example: for (let value of arr) {...}

23
Q

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

A

for...in iterates over keys (indexes), for...of iterates over values.

24
Q

How do you exit multiple nested loops?

A

Use labels with break. Example: break outerLoop;

25
How can you iterate with a function instead of a loop?
Use array methods like `.map()`, `.filter()`, or `.reduce()`.
26
What is an infinite loop?
A loop that never ends because the condition always evaluates to true.
27
How can you avoid infinite loops?
Always ensure your loop condition will eventually become false.
28
What does `Array.prototype.forEach()` do?
Executes a callback once for each array element.
29
Can you use `break` in `.forEach()`?
No, `.forEach()` cannot be stopped early; use a regular `for` loop or `.some()` instead.
30
What’s a practical use for loops?
Iterating over data, rendering UI elements, or performing repetitive calculations.
31
What’s a practical use for conditionals?
Making decisions in code — e.g., displaying messages, validating input, or controlling flow.
32
What is nesting in control structures?
Placing one loop or conditional inside another for complex logic. Example: `if(x){ for(...) {...} }`