Skip to content
Home » Blogs » Conditional Statements In JavaScript | JavaScript Tutorial #2

Conditional Statements In JavaScript | JavaScript Tutorial #24 min read

JavaScript Tutorial 2 3

In the Previous JavaScript Course We Started With JavaScript, If You Have Missed It Access it Here: Getting Started With JavaScript. So In this Post, we will proceed further with Conditional Statements Used In JavaScript.

If Statement

In JavaScript, you can use conditional statements to execute different pieces of code depending on whether a certain condition is true or false.

The most basic conditional statement is the `if `statement, which allows you to execute a block of code only if a certain condition is true. Here is an example:

Else Statement

You can also use an `else` clause to execute a different block of code if the condition is false. For example:

if (x > y) {

// Execute this code if x is greater than y

} else {

// Execute this code if x is not greater than y

}

Else If Statement

You can use as many `else if ` clauses as you need to test for multiple conditions. For example:

if (x > y) {
// Execute this code if x is greater than y
} else if (x < y) {
// Execute this code if x is less than y
} else {
// Execute this code if x is equal to y
}

It is important to use proper indentation to make your code easy to read and understand.

For Loop

In JavaScript, you can use loops to repeat a block of code a certain number of times or until a certain condition is met.

The `for` loop is used to execute a block of code a predetermined number of times. Here is the basic syntax:

for (initialization; condition; update) {
// Code to be executed
}

The initialization statement is executed before the loop starts and is usually used to declare a counter variable. The condition is evaluated before each iteration of the loop, and the loop will continue as long as the condition is true. The update statement is executed at the end of each iteration and is usually used to update the counter variable.

Here is an example of a for loop that counts from 1 to 10:

for (var i = 1; i <= 10; i++) {
console.log(i);
}

While Loop

The `while` loop is used to execute a block of code as long as a certain condition is true. Here is the basic syntax:

while (condition) {
// Code to be executed
}

It is important to include a statement inside the loop that will eventually make the condition false, otherwise, the loop will run forever (this is known as an infinite loop).

Here is an example of a while loop that counts from 1 to 10:

var i = 1;
while (i <= 10) {
console.log(i);
i++;
}

Both for and while loops can be controlled using the break and continue statements. The break statement will exit the loop prematurely, while the continue statement will skip the rest of the current iteration and move on to the next one.

Functions (declaring, calling, and returning)

In JavaScript, a function is a block of code that is defined once and can be executed multiple times. Functions are an important concept in programming, as they allow you to write code that is modular, reusable, and easier to read and understand.

To declare a function in JavaScript, you use the `function` keyword followed by the name of the function, a list of parameters (enclosed in parentheses), and a block of code (enclosed in curly braces). Here is an example:

function greet(name) {
console.log(“Hello, ” + name + “!”);
}

To call a function, you use the name of the function followed by a list of arguments (enclosed in parentheses). For example:

greet(“Alice”); // Outputs “Hello, Alice!”
greet(“Bob”); // Outputs “Hello, Bob!”

You can also return a value from a function using the return statement. For example:

function add(x, y) {
return x + y;
}
var sum = add(5, 3); // sum will be 8

Functions can also be declared using the function expression syntax. In this syntax, you assign a function to a variable. Here is an example:

var greet = function(name) {
console.log(“Hello, ” + name + “!”);
};

Function expressions are often used when defining anonymous functions, which are functions that do not have a name.

It is good practice to include comments in your code to explain what the code does and how it works. In JavaScript, you can use single-line comments (//) and multi-line comments (/* */) to add comments to your code.

Tanmay Sinha

Leave a Reply