Conditional statements in JS

  1. if/else:

    The basic conditional structure that executes code blocks based on whether a condition evaluates to true and false.

  2. switch:

    Used for multiple conditions. It is cleaner and more readable than multiple if / else if statements in some cases

  3. Ternary Conditional:

    A shorthand for if-else.

    syntax: condition ? expressionIfTrue : epressionIfFalse

Loops

for loops

The classic loop, allowing fine-grained control over initialization, condition, and iteration.

while loops

Repeats as long as the condition is true. Common Pitfall: Infinite loops if a condition is not properly updated.

do...while loops

Executes the block at least once before checking the condition.

for...in

Loops through the keys (or property names) of an object or array (as an object). It works for objects, arrays, and array-like structures, but the keys are the indices when used on arrays.

Example:

const obj = { a : 1, b : 2, c : 3 };
for (let key in obj) {
	console.log(key); // a b c
}

for...of

Loops through the values of iterable objects (like arrays, strings, or maps). It does not give the index or key.

Example: