0% found this document useful (0 votes)
2 views4 pages

Js Control Statements

js
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Js Control Statements

js
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

JavaScript Control Statements - Syntax & Examples

If :
Syntax:
if (condition) {
// code
}

Example:
let age = 20;
if (age >= 18) {
[Link]("You are an adult");
}

if...else :
Syntax:
if (condition) {
// code if true
} else {
// code if false
}

Example:
let age = 16;
if (age >= 18) {
[Link]("You can vote");
} else {
[Link]("You cannot vote");
}

if...else if...else :
Syntax:
if (condition1) {
// code
} else if (condition2) {
// code
} else {
// code
}

Example:
let marks = 75;
if (marks >= 90) {
[Link]("Grade A");
} else if (marks >= 60) {
[Link]("Grade B");
} else {
[Link]("Grade C");
}

Nested if :
Syntax:
if (condition1) {
if (condition2) {
// code
}
}

Example:
let age = 25;
let hasID = true;
if (age >= 18) {
if (hasID) {
[Link]("You can enter");
}
}

Do :
Syntax:
do {
// code
} while (false);

Example:
do {
[Link]("Runs once no matter what");
} while (false);

do...while :
Syntax:
do {
// code
} while (condition);

Example:
let i = 1;
do {
[Link](i);
i++;
} while (i <= 3);

While :
Syntax:
while (condition) {
// code
}

Example:
let i = 1;
while (i <= 3) {
[Link](i);
i++;
}

Switch :
Syntax:
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}

Example:
let day = 2;
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
default:
[Link]("Other day");
}
for Loop :

Syntax :

for (initialization; condition; update) {

// code

Example :

for (let i = 1; i <= 5; i++) {

[Link](i);

// Output: 1 2 3 4 5

You might also like