Tech Study

JavaScript if else Statement

JavaScript if…else Statement

The javascript if else statement in JavaScript is a conditional statement that executes a block of code if a specified condition is true, and another block of code if the condition is false.

  • if statement

if statement is the most basic conditional statement. It implements a block of code only when the given condition is true.If the condition is false then implementation leaves the block and jumps to the next section.

Syntax:

if (condition) {
//code
}

 

  • else statement 

The else statement implements a code block when the condition in the if statement is false.else statement can’ not be used alone it must be used after the if statement.

Syntax:

if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}

The else block is alternative, and you can also chain multiple conditions using else if, like this:

if (condition 1) {
// code to execute if condition 1 is correct
} else if (condition 2) {
// code to execute if condition 2 is correct
} else {
// code to implement if both condition 1 and condition 2 are false
}

Example:

// Define a variable with a number value
let num = 5;
// Check if the number is larger than 0
if (num > 0) {
console.log("The number is positive");
} else {
console.log("The number is negative or zero");
}

Output:

The number is positive

In this program, we define a variable num with a value of 5. We then use a javascript if else statement to check if the value of num is larger than 0. If it is, we log a message to the console saying “The number is positive”. If it’s not, we log a message saying “The number is negative or zero”. You can try changing the value of num and see how the program behaves.

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more