Computer Science

Javascript Switch Statement

A JavaScript switch statement is a control flow mechanism used to execute different code blocks based on the value of a variable or expression. It provides a concise way to handle multiple possible conditions within a program. The switch statement evaluates an expression and compares it with multiple case values to determine the appropriate code block to execute.

Written by Perlego with AI-assistance

3 Key excerpts on "Javascript Switch Statement"

Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.
  • Confident Web Design
    eBook - ePub

    Confident Web Design

    Master the Fundamentals of Website Creation and Supercharge Your Career

    • Kenny Wood(Author)
    • 2018(Publication Date)
    • Kogan Page
      (Publisher)

    ...For this, we need the switch statement. The switch statement This conditional statement is perfect for situations where we have a large number of different actions that should be performed based on different conditions being met. The switch statement has different syntax to anything we have seen before. It can seem a little strange at first, so let’s waste no time in getting familiar with it. Let’s see some code: var number = 15; switch(number) { case 5: console.log(‘number is 5’); break; case 10: console.log(‘number is 10’); break; case 15: console.log(‘number is 15’); } Looking at the above code, there’s a lot of new syntax to analyse here, so let’s work our way from top to bottom. We start with the keyword switch followed by parenthesis. Inside the parenthesis we enter our expression. (The expression is the value that we want to evaluate our cases against. So in this case we just enter the variable number. This means that every case inside the switch statement will be evaluated against the number 15.) Then we move down to the case keyword followed by the test case, so in the first example we are using ‘case 5’. This is saying: ‘if the number is 5 then this case is true.’ This is followed by a colon, not a semi-colon, which is something we have only seen before in objects. Then on a new line we enter our code that we want to run when the case is true. After we finish writing our code, we follow it up with a break keyword followed by a semi-colon. The break keyword instructs JavaScript to ‘break’ out of the switch block and stop the execution of any more code inside the switch statement. After our break keyword, we can write another case directly below, with the same syntax. During execution of the code, JavaScript will handle the switch statement very much like an if else statement...

  • HTML, CSS & JavaScript in easy steps

    ...17 Manage the Script Flow This chapter describes the JavaScript control structures and demonstrates how they control the progress of scripts. Branch If Branch Alternatives Switch Alternatives Loop For Loop While Do Loops Break Out Catch Errors Summary Branch If The progress of any script or computer program depends upon the evaluation of conditions to determine the direction of flow. Each evaluation may present one or more branches along which to continue according to the result of the evaluation. In JavaScript, the basic conditional test is performed with the if keyword to test a condition for a Boolean true or false value. When the result is true, a statement following the evaluation will be executed, otherwise this is skipped and flow continues at the next subsequent statement. The syntax of an if statement demands that the condition to be tested is placed within parentheses after the if keyword, and looks like this: if (condition) execute-this-statement-when-true An if statement may also specify multiple statements to be executed when the result is true by enclosing those statements within braces, like this: if (condition) { execute-this-statement-when-true execute-this-statement-when-true execute-this-statement-when-true } The evaluation of a condition and the execution of actions according to its result simply reflects the real-life thought process – for example, the actions you might execute on a summer day: let temperature = readThermometer () const tolerable = 25 if (temperature > tolerable) { turn_on_air-conditioning () get_a_cool_drink () stay_in_shade () } The conditional test is...

  • Hack Audio
    eBook - ePub

    Hack Audio

    An Introduction to Computer Programming and Digital Signal Processing in MATLAB

    • Eric Tarr(Author)
    • 2018(Publication Date)
    • Routledge
      (Publisher)

    ...Similar to an if statement, only certain commands are executed during the program depending on the conditions of the switch statement. A switch statement evaluates whether a variable is equal to the variable in each case. If it is TRUE, the statements in that case are executed. Otherwise, the program moves on to testing the next case. Examples: Save and run the following m-file in MATLAB. % This script is an example of using a "switch" statement % as a conditional statement a = 5; % Variable for switch cases switch a case -5 % "a" does not equal -5 b = 1 % Skip this case case 5 % "a" does equal 5 b = 2 % Execute this case end Example 5.8: switch conditional statement Comparison of if and switch Statements The if and switch conditional statements can be used to accomplish identical flow in a computer program. Therefore, in most cases it is the preference of the programmer to decide which type of statement to use. As a matter of best practice, a programmer should use the conditional statement that allows for the easiest interpretation of the written code. 5.3.2 Loops One task computers excel at performing is the repetition of instructions. As a programmer, if you want to repeat a command multiple times, you could copy and paste the command manually. Another option is to create a loop. A loop repeats the included commands until certain requirements are met. Requirements are defined in the command used to create the loop, also called the loop declaration. for Statement One type of loop is declared in MATLAB using the for statement. Following the for keyword, an expression is needed to define a counting variable to set the number of times the loop repeats. This variable can be referenced during the repetitions of the loop...