[go: up one dir, main page]

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

Conditional Statement

The document provides an overview of conditional statements in JavaScript, including 'if', 'if-else', nested 'if', and 'switch' statements. It includes example code snippets demonstrating how to implement each type of conditional statement for various scenarios, such as time-based greetings and day identification. Additionally, it showcases the use of a default case in a switch statement for handling unexpected inputs.

Uploaded by

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

Conditional Statement

The document provides an overview of conditional statements in JavaScript, including 'if', 'if-else', nested 'if', and 'switch' statements. It includes example code snippets demonstrating how to implement each type of conditional statement for various scenarios, such as time-based greetings and day identification. Additionally, it showcases the use of a default case in a switch statement for handling unexpected inputs.

Uploaded by

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

Conditional statement:

• If
• If-else
• Nested else(if,else if,else)
• Switch

If statement:

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript if .. else</h2>

<p>A time-based greeting:</p>

<p id="demo"></p>

<script>

const hour = 11

let greeting;

if (hour < 18) {

greeting = "Good day";

document.getElementById("demo").innerHTML = greeting;

</script>

If -else statement:

<!DOCTYPE html>
<html>

<body>

<h2>JavaScript if .. else</h2>

<p>A time-based greeting:</p>

<p id="demo"></p>

<script>

const hour = 11

let greeting;

if (hour < 18) {

greeting = "Good day";

} else {

greeting = "Good evening";

document.getElementById("demo").innerHTML = greeting;

</script>

</body>

</html>

Nested statement(if,elseif,else):

<html>

<body>
<h2>JavaScript if .. else</h2>

<p>A time-based greeting:</p>

<p id="demo"></p>

<script>

const time = 9

let greeting;

if (time < 10) {

greeting = "Good morning";

} else if (time < 20) {

greeting = "Good day";

} else {

greeting = "Good evening";

document.getElementById("demo").innerHTML = greeting;

</script>

</body>

</html>

Switch statement:

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript switch</h2>
<p id="demo"></p>

<script>

let day;

switch (new Date().getDay()) {

case 0:

day = "Sunday";

break;

case 1:

day = "Monday";

break;

case 2:

day = "Tuesday";

break;

case 3:

day = "Wednesday";

break;

case 4:

day = "Thursday";

break;

case 5:

day = "Friday";

break;

case 6:

day = "Saturday";

document.getElementById("demo").innerHTML = "Today is " + day;

</script>
</body>

</html>

Default statement:

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript switch</h2>

<p id="demo"></p>

<script>

let text;

switch (new Date().getDay()) {

case 6:

text = "Today is Saturday";

break;

case 0:

text = "Today is Sunday";

break;

default:

text = "Looking forward to the Weekend";

document.getElementById("demo").innerHTML = text;

</script>
</body>

</html>

Eg1:

<!doctype html>

<html>

<head>

<title>switch</title>

</head>

<script>

let fruits= prompt(“enter the name of fruits”)

switch(fruits) {
case "Banana":
alert("Hello")
break;
case "Apple":
alert("Welcome")
break;
}

</script>

</html>

</body>

You might also like