Diving into the JavaScript fundamentals Part:2, #100DaysofCode

Diving into the JavaScript fundamentals Part:2, #100DaysofCode

Day 2 of #100daysofcode challenge

ยท

4 min read

Greetings, fellow coding enthusiasts! ๐Ÿš€ Today marks Day 2 of my "100 Days of Code" journey, and I'm excited to delve into the concept of control flow and logical operations in JavaScript. Join me as I explore the intricacies of loops, conditionals, and more.

Loops:

For Loop

A for loop is ideal when you know the exact number of iterations you need. It consists of an initialization, a condition, and an iteration statement.

Syntax:

for (initialization; condition; iteration) {
  // code to execute in each iteration
}

Example:

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

While Loop

A while loop repeatedly executes a block of code as long as the specified condition remains true.

Syntax:

while (condition) {
  // code to execute while the condition is true
}

Example:

let count = 0; // initialization
while (count < 5) {
  console.log(count);
  count++; // iteration
}

Do While Loop

A do while loop is similar to a while loop, but it ensures that the code block is executed at least once before checking the condition.

Syntax:

do {
  // code to execute
} while (condition);

Example:

let num = 0; // initialization
do {
  console.log(num);
  num++; // iteration
} while (num < 5);

Conditional Statements:

if / else if / else

Conditional statements help control the flow of your code based on specified conditions.

Syntax:

if (condition) {
  // code to execute if condition is true
} else if (anotherCondition) {
  // code to execute if anotherCondition is true
} else {
  // code to execute if no conditions are true
}

Example:

let age = 18;
if (age < 18) {
  console.log("You are a minor.");
} else if (age >= 18 && age < 60) {
  console.log("You are an adult.");
} else {
  console.log("You are a senior citizen.");
}

Nested if Statement

Nested-if statements are used when you have multiple conditions to evaluate within other conditions.

Syntax:

if (condition1) {
  if (condition2) {
    // code to execute if both conditions are true
  }
}

Example:

let num = 15;
if (num > 10) {
  if (num < 20) {
    console.log("The number is between 10 and 20.");
  }
}

Break and Continue:

Break Statement

The break statement allows you to exit a loop prematurely based on a specific condition.

Syntax:

for each item in a collection {
  if (condition) {
    // exit the loop
    break;
  }
}

Example:

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

Continue Statement

The continue statement skips the current iteration of a loop and moves to the next one.

Syntax:

for each item in a collection {
  if (condition) {
    // skip this iteration
    continue;
  }
  // code to execute if condition is not met
}

Example:

for (let i = 0; i < 10; i++) {
  if (i % 2 === 0) {
    continue;
  }
  console.log(i);
}

Logical Operators: Making Logical Decisions

JavaScript offers three logical operators: && (AND), || (OR), and ! (NOT).

AND Operator (&&): Returns true if both operands are true.

OR Operator (||): Returns true if at least one operand is true.

NOT Operator (!): Inverts the value of an operand.

let age = 25;
let isStudent = true;

if (age >= 18 && !isStudent) {
  console.log("You are an adult.");
} else if (age < 18 || isStudent) {
  console.log("You are a minor or a student.");
}

Variable and Block Scope: Controlling Access

The scope of a variable determines where it can be accessed within your code.

{
  // This is a block scope
  let insideBlock = "I am inside the block";
}

// Cannot access insideBlock here

Ternary Operator: A Compact Decision Maker

The ternary operator provides a concise way to write if/else statements.

Syntax and Example:

condition ? valueIfTrue : valueIfFalse;
let age = 20;
let status = age >= 18 ? "Adult" : "Minor";
console.log(status); // Output: "Adult"

Switch Statement

: Multi-Option Decision

A switch statement allows you to perform different actions based on different conditions.

Syntax and Example:

switch (expression) {
  case value1:
    // code to execute if expression equals value1
    break;
  case value2:
    // code to execute if expression equals value2
    break;
  // ...
  default:
    // code to execute if none of the cases match
}
let day = "Wednesday";
switch (day) {
  case "Monday":
    console.log("It's the start of the week.");
    break;
  case "Wednesday":
    console.log("It's the middle of the week.");
    break;
  default:
    console.log("It's another day of the week.");
}

Wrapping Up

Day 2 has been a thrilling exploration into the world of control flow, logical operators, and conditional statements in JavaScript. I learned that loops help us iterate through data, decision-making tools to control the program's flow, and logical operators for making smart comparisons.

Stay tuned for more coding articles as I continue my 100 Days of Code journey! Remember, every line of code you write is a step closer to mastery. Keep coding! ๐Ÿ’ป๐ŸŽ‰

Connect with me on Twitter


ย