GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • About
  • Reference
  • Integrations
  • API
  • Blog
  • JavaScript Syntax Reference
  • Feedback

This view

  • Original page
  • New address

GeezerVison is showing syntaxdb.com — Switch Case in JavaScript - SyntaxDB - JavaScript Syntax Reference

  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Languages
    • User Program Communication
      • Console Output
      • Commenting
      • Read from Text File
    • Variables
      • Variable Declaration
      • Global Variables
      • Arrays
      • Determine Type
    • Control Flow
      • If Statement
      • Switch Case
      • Ternary Operator
      • For Loop
      • For Each
      • While Loop
      • Do-While Loop
      • Try Catch Block
    • Functions
      • Function Declaration
      • Anonymous Functions
      • Closures
      • Function Assignment
    • Objects
      • Object Declaration
      • Methods
      • Create Object using Constructor
      • Constructor
Categories
Language
JavaScript
  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Version: 1.8.5

Categories

  • User Program Communication
    • Console Output
    • Commenting
    • Read from Text File
  • Variables
    • Variable Declaration
    • Global Variables
    • Arrays
    • Determine Type
  • Control Flow
    • If Statement
    • Switch Case
    • Ternary Operator
    • For Loop
    • For Each
    • While Loop
    • Do-While Loop
    • Try Catch Block
  • Functions
    • Function Declaration
    • Anonymous Functions
    • Closures
    • Function Assignment
  • Objects
    • Object Declaration
    • Methods
    • Create Object using Constructor
    • Constructor

Control Flow

Switch Case in JavaScript

A switch case is used test variable equality for a list of values, where each value is a case. When the variable is equal to one of the cases, the statements following the case are executed.


Syntax
switch(variable) { 
    case valueOne: 
        //statements 
        break; 
    case valueTwo: 
        //statements
        break; 
    default: //optional
    //statements
}

Notes

A break statement ends the switch case. The optional default case is for when the variable does not equal any of the cases.


Example
var choice = 'Y';
switch(choice) { 
    case 'Y' : 
        console.log("Yes"); 
        break; 
    case 'M' : 
        console.log("Maybe"); 
        break; 
    case 'N' : 
        console.log("No"); 
        break; 
    default: 
        console.log("Invalid response"); 
}

See Also
Documentation

switch - JavaScript | MDN

Add to Slack

< If Statement   |   Ternary Operator >


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.