DEV Community

Narmatha
Narmatha

Posted on

CONDITION STATEMENT IN JAVASCRIPT

CONDITION STATEMENT IN JAVASCRIPT

A conditional statement in JavaScript is a programming statement that is used to make decisions in a program based on whether a particular condition is true or false. It allows the program to control the flow of execution by checking a condition and then executing a specific block of code depending on the result of that condition.

IF STATEMENT:

The if statement is a conditional statement used to execute a block of code only when a specified condition is true.

Syntax

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

EXAMPLE

let age = 20;

if (age >= 18) {
console.log("You are an adult");
}

IF ELSE STATEMENT:

The if...else statement in JavaScript is a conditional statement that is used to execute one block of code if a condition is true and another block of code if the condition is false.

Syntax:

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

`

EXAMPLE

`let age = 16;

if (age >= 18) {
console.log("You are an adult");
} else {
console.log("You are a minor");
}`

Top comments (0)