Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Switch Statements

User image

Published by

sanya sanya

Published at: 6th May, 2023
1.07 mins read

The Switch Statement in javascript is used to perform operations based on certain conditions. It has the same working as that of the Else-if condition.

There are cases in the switch statement and as per that cases, different conditions are made to execute.


The code showing the working of the switch statement is mentioned below -

let age = 2; switch (age) { case 1: age = 'Twenty'; break; case 2: a = 'Twenty Two'; break; default: a = 'Very Young'; break; } console.log(The value is ${a});
//Output: The value is Twenty-Two

Working of Switch Statement

The working of the Switch Statement is described in the points mentioned below -

  1. The evaluation of the switch will take only once.
  2. The main value of the expression will be matched with all the cases.
  3. The matched block will be executed.
  4. Unmatched block will not be executed.
  5. If multiple cases get matched, the first case will be selected.
  6. If no case matched, the default case will execute.

Break and Default Keywords

When the break keyword is written below the case, the execution of the switch block will be stopped.

The default keyword specifies the code that will be executed when there is no match in the switch block.

Library

WEB DEVELOPMENT

Basic

HTML - Hyper Text Markup Language

CSS - Cascading Style Sheets

JavaScript

An Introduction to Javascript!

How to Run JavaScript Code

Variables in Javascript

Numbers in JavaScript

JavaScript Operators

Data Types in JavaScript

Conditional Statements

Switch Statements

Loops in Javascript

Arrays in JavaScript

Strings in JavaScript

Objects in JavaScript

Object Methods in JavaScript

Functions in JavaScript

Object Referencing and Copying in JavaScript

' this' keyword

Asynchronous Programming in JavaScript

Callbacks in JavaScript

Promises in JavaScript

Constructor Functions in JavaScript

Async and Await in JavaScript

Type Conversion in Javascript

DOM

Currying in JavaScript

Network Request

Frontend

Backend

Interview Questions

FAANG QUESTIONS

On this page

Working of Switch Statement

Break and Default Keywords