Switch in JavaScript - learnit

Home Top Ad

Post Top Ad

Monday, July 20, 2020

Switch in JavaScript

Switch in JavaScript

What is JavaScript?

Welcome to the MDN beginner's JavaScript course! In this article we will look at JavaScript from a high level, answering questions such as "What is it?" and "What can you do with it?", and making sure you are comfortable with JavaScript's purpose.


Prerequisites Basic computer literacy, a basic understanding of HTML and CSS.
Objective: To gain familiarity with what JavaScript is, what it can do, and how it fits into a web site.

A high-level definition

JavaScript is a scripting or programming language that allows you to implement complex features on web pages — every time a web page does more than just sit there and display static information for you to look at — displaying timely content updates, interactive maps, animated 2D/3D graphics, scrolling video jukeboxes, etc. — you can bet that JavaScript is probably involved. It is the third layer of the layer cake of standard web technologies, two of which (HTML and CSS) we have covered in much more detail in other parts of the Learning Area.


CSS is a language of style rules that we use to apply styling to our HTML content, for example setting background colors and fonts, and laying out our content in multiple columns.


HTML is the markup language that we use to structure and give meaning to our web content, for example defining paragraphs, headings, and data tables, or embedding images and videos in the page.


JavaScript is a scripting language that enables you to create dynamically updating content, control multimedia, animate images, and pretty much everything else. (Okay, not everything, but it is amazing what you can achieve with a few lines of JavaScript code.)


Switch in JavaScript

    The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case.

Syntax

switch(n)
case value1:
//Statements executed when the
//result of expression matches value1
[break;]
case value2:
//Statements executed when the
//result of expression matches value2
[break;]
...
case valueN:
//Statements executed when the
//result of expression matches valueN
[break;]
[default:
//Statements executed when none of
//the values match the value of the expression
[break;]]
}

Description

    the associated statements. (If multiple Cases match the provided value, the first Case that matches is selected, even if the Cases are not equal to each other.)


    If no matching Case clause is found, the program looks for the optional Default clause, and if found, transfers control to that clause, executing the associated statements. If no Default clause is found, the program continues execution at the statement following the end of Switch. By convention, the Default clause is the last clause, but it does not need to be so.


    The optional Break statement associated with each Case label ensures that the program breaks out of Switch once the matched statement is executed and continues execution at the statement following Switch. If break is omitted, the program continues execution at the next statement in the Switch statement. The break statement is not required if a Return statement precedes it.


Example

<html>
<head>
<title></title>
</head>
<body>
<h1><p id="test"> </p></h1>
<script>
var day;
switch(new Date().getDay()){
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";
break;
case 0:
day="Sunday";
}
document.getElementById("test").innerHTML="today is "+day;
</script>
</body>
</html>

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad