answersLogoWhite

0


Best Answer

The JavaScript switch statement is nothing more than a easy replacement for a series of if-then-else statements. Here's an example.

var chicken;

switch( chicken ) {

case "fried":

console.log('Fried chicken!');

break;

case "fricassee":

console.log('Fricassee!!!!!!!!');

break;

case "egg":

console.log('Which came first?');

break;

default:

console.log( 'With its head cut off.' );

}//end switch

This statement takes a variable, then compares it to the values of each "case." When it finds a matchin value, it executes the code in that case statement. It will execute until it hits the "break" statement, so if you skip a break, then the code will continue straight through.

This is the same structure as:

if ( chicken "egg" ){

console.log('Which came first?')

}else{

console.log( 'With its head cut off.' );

}

As you can see, the switch statement is simpler, easier to read, and easier to maintain.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

If in Java in serves switching between cases.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What does the switch statement in JavaScript do?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are control structures used in javascript?

The control structures used in java script are if-statement, for-loop, for-in loop, while loop,do-while loop, switch-statement, with-statement. try-catch-finally statements.


Can a continue statement be used in a switch statement?

If you have a loop in your switch statement or around your switch statement, you can use the continue statement in that. You cannot use a continue statement outside of a loop (do, for, or while).


What symbol is used to indicate the end of each JavaScript statement?

The semi-colon ( ; ) is used to indicate the end of a statement in JavaScirpt.


What is the Javascript switch used for?

The Javascript switch is used for performing different actions based on various conditions. It allows one to produce more complex programs and the software to respond to different situations it may face.


What are non-executable statement in a program javascript?

Comments are non-executable statements within JavaScript (or any other programming.) In JavaScript, comments are surrounded by /* and */ for multi-line comments, or started with // for single line comments.


What is the difference between 'switch' statement and 'if' statement?

we can use switch statement in multiple time but in if statement we can not use multiple time


What is the deffernce of the switch statement and the if statement in c plus plus?

If statement is single selection statement,whereas the switch statement is multiple selective.


What is a JavaScript statement?

A statement in a programming language is a standalone unit of code. In JavaScript, statements generally end with a semicolon or a closing bracket.Examples:// variable statementvar x = 5;// if statementif (x == 0) {...}The ECMAScript language specification lists all the different types of statements permitted in JavaScript. See related links.


What does default mean in switch statement?

Default clause in switch statement used to indicate that the desired option is not available with the switch case statement. it is similar to else statement of if statement which is used when the condition does not satisfy.


How does form validation differ in JavaScript and PHP?

In JavaScript the validation is done client side, which means it can be easily bypassed by turning JavaScript off in your browser. But with PHP it is server side, which means you can't switch it off, so you can't bypass the validation.


What is the use of switch statement in java?

In java, a switch statement is used to simplify a long list of 'if' statements. A switch statement takes the form of:switch (variableName){case condition1; command1;case condition2; command2;...}


How do you break a loop in a switch case statement?

using break; statement