In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use Switch sentences in JavaScript". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Switch sentences in JavaScript.
Switch
The switch statement evaluates the expression and executes the code as a result of matching case. It looks a little daunting at first, but the basic syntax is similar to the if statement. It will always be written in switch () {}, with parentheses containing the expression to test, and curly braces containing the potential code to execute.
The following is an example of a switch statement with two case statements and a fallback called default.
Switch (expression) {case x: / / execute case x code block break; case y: / / execute case y code block break; default: / / execute default code block}
Following the logic of the code block above, this is the sequence of events that will occur:
The expression is evaluated
The first case,x will test against expressions. If there is a match, the code executes and the break keyword ends the switch block.
If it does not match, x will skip and y will test the case against the expression. If y matches the expression, the code executes and exits the switch block.
If all cases do not match, the default code block will run.
Let's make a working example of the switch statement according to the above syntax. In this code block, we will use the new Date () method to find the current date of the week and use getDay () to print the number corresponding to that day. 1 stands for Monday, and always stands for 7 for Sunday. We'll start by setting variables.
Const day = new Date () .getDay ()
Using switch, we will send messages to the console every day of the week. The program runs from top to bottom to find a match, and once one is found, the break command stops the switch block to continue the evaluation statement.
Week.js
/ / Set the current day of the week to a variable, with 1 being Monday and 7 being Sundayconst day = new Date (). GetDay (); switch (day) {case 1: console.log ("Happy Monday!"); break; case 2: console.log ("It's Tuesday. You got this!); break; case 3: console.log ("Hump day already!"); break; case 4: console.log ("Just one more day 'til the weekend!"); break; case 5: console.log ("Happy Friday!"); break Case 6: console.log ("Have a wonderful Saturday!"); break; case 7: console.log ("It's Sunday, time to relax!"); break; default: console.log ("Something went horribly wrong...");} Output'Just one more day 'til the endurance'
This code was tested on Thursday and corresponds to 4, so the console output is Just one more day 'til the weekend!. Your output will vary depending on the day of the week in which you test the code. Our default includes a block at the end to run when an error occurs, which should not happen in this case, because there are only seven days in a week. For example, we may only have print results from Monday to Friday, and the default block may have the same information over the weekend.
If we omit the break keyword in each statement, no other case statement will be evaluated as true, but the program will continue to check until it reaches the end. To make our program faster and more efficient, we include break.
Switch Ranges
In some cases, you need to evaluate a series of values in the switch block, rather than a single value like the one in the example above. We can do this by setting the expression to true and performing the action in each case statement.
To make this easier to understand, we have created a simple scoring application that will get a numeric score and convert it to an alphabetical rating with the following requirements.
● level 90 and above is A
● level 80 to 89 is B
● levels 70 to 79 are C.
● levels 60 to 69 are D.
● level 59 or below is F
Now we can write it as a switch statement. Since we are checking the scope, we will perform an operation in each case to see if each expression is being evaluated as true, and then break the statement after meeting the true requirements.
Grades.js
/ / Set the student's gradeconst grade = 87 console.log switch (true) {/ / If score is 90 or greater case grade > = 90: console.log ("A"); break; / / If score is 80 or greater case grade > = 80: console.log ("B"); break; / / If score is 70 or greater case grade > = 70: console.log ("C") Break; / / If score is 60 or greater case grade > = 60: console.log ("D"); break; / / Anything 59 or below is failing default: console.log ("F");} Output'B'
In this example, the expression in parentheses to evaluate is true. This means that any situation evaluated as true is a match.
Just like with else, switch evaluates from top to bottom and accepts the first real match. So, even if our level variable is 87, so it is also evaluated as true for C and D, the first match is B, which will be the output.
Multiple Cases
You may encounter multiple case code that should have the same output. To achieve this, you can use multiple case for each code block.
To test this, we will make a small application that matches the current month with the appropriate season. First, we will use the new Date () method to find the number corresponding to the current month and apply it to the month variable.
For simplicity, our application will output four seasons with the following specifications:
● Winter: January, February and March
● Spring: April, May and June
● Summer: July, August and September
● Autumn: October, November and December
Here is our code.
Seasons.js
/ Get number corresponding to the current month, with 0 being January and 11 being Decemberconst month = new Date (). GetMonth (); switch (month) {/ / January, February, March case 0: case 1: case 2: console.log ("Winter"); break; / / April, May, June case 3: case 4: case 5: console.log ("Spring") Break; / / July, August, September case 6: case 7: case 8: console.log ("Summer"); break; / / October, November, December case 9: case 10: case 11: console.log ("Autumn"); break Default: console.log ("Something went wrong.");}
When we run the code, we will receive the output that identifies the current season according to the above specification.
OutputSummer
The month at the time of publication is 8, which corresponds to a case statement produced in the "summer" season.
At this point, I believe you have a deeper understanding of "how to use Switch statements in JavaScript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.