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

Kartik Tummawar
4 min readAug 14, 2023

--

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.

for (initialization; condition; iteration) {
// code to execute in each iteration
}
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.

while (condition) {
// code to execute while the condition is true
}
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.

do {
// code to execute
} while (condition);
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.

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
}
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.

if (condition1) {
if (condition2) {
// code to execute if both conditions are true
}
}
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.

for each item in a collection {
if (condition) {
// exit the loop
break;
}
}
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.

for each item in a collection {
if (condition) {
// skip this iteration
continue;
}
// code to execute if condition is not met
}
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.

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.

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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Kartik Tummawar
Kartik Tummawar

Written by Kartik Tummawar

0 Followers

A tech enthusiast currently running my #100DaysOfCode series,Follow along as I explore the world of web development, and document my tech journey.

No responses yet

Write a response