Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Switch Case in JavaScript

Uploaded by

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

Switch Case in JavaScript

Uploaded by

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

Switch case In

JavaScript
By Kanwal Mustafa
Basic Switch Case:

let fruit = 'Apple';

let taste;

switch (fruit) {

case 'Banana':

taste = 'Sweet';

break;

case 'Lemon':

taste = 'Sour';

break;

default:

taste = 'Unknown';

document.write(taste);
Using Default Case:
let day = 3;

let dayName;

switch (day) {

case 1:

dayName = 'Monday';

break;

case 2:

dayName = 'Tuesday';

break;

case 3:

dayName = 'Wednesday';

break;

default:

dayName = 'Invalid day';

document. Write(dayName);
Using Expressions in Case
Labels:
let grade = 'B';
let feedback;

switch (grade) {
case 'A':
feedback = 'Excellent!';
break;
case 'B':
feedback = 'Good job!';
break;
case 'C':
feedback = 'Needs improvement.';
break;
default:
feedback = 'Not graded yet.';
}

Document.write(feedback);
Using Switch Case with Strings:

let color = 'red';

let description;

switch (color) {

case 'red':

description = 'Color of blood.';

break;

case 'blue':

description = 'Color of the sky.';

break;

default:

description = 'Unknown color.';

Document.write(description);
Using Switch Case with Object
Values:
let day = 'Monday';

let schedule;

switch (day) {

case 'Monday':

case 'Wednesday':

case 'Friday':

schedule = 'Math class';

break;

case 'Tuesday':

case 'Thursday':

schedule = 'Science class';

break;

default:

schedule = 'No classes';

Document.write(schedule);
Nested Switch Case:
let x = 1;
let y = 2;
let operation = 'addition';
let result;
switch (operation) {
case 'addition':
result = x + y;
break;
case 'subtraction':
result = x - y;
break;
case 'multiplication':
case 'division':
switch (operation) {
case 'multiplication':
result = x * y;
break;
case 'division':
result = x / y;
break;
default:
result = 'Invalid operation’;}
break;
default:
result = 'Invalid operation’;}

You might also like