From 6cbe6aa8b9580062918d3980e03a719b57f0a71d Mon Sep 17 00:00:00 2001 From: Charishma-Bailapudi <20131a4410@gvpce.ac.in> Date: Wed, 26 Oct 2022 22:47:31 +0530 Subject: [PATCH] added switch statement --- 2-js-basics/3-making-decisions/README.md | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/2-js-basics/3-making-decisions/README.md b/2-js-basics/3-making-decisions/README.md index 738772a5..4fbe89c4 100644 --- a/2-js-basics/3-making-decisions/README.md +++ b/2-js-basics/3-making-decisions/README.md @@ -81,6 +81,45 @@ else{ ✅ Test your understanding of this code and the following code by running it in a browser console. Change the values of the currentMoney and laptopPrice variables to change the returned `console.log()`. + +## Switch Statement + +The `switch` statement is used to perform different actions based on different conditions.Use the `switch` statement to select one of many code blocks to be executed. + +```javascript +switch(expression) { + case x: + // code block + break; + case y: + // code block + break; + default: + // code block +} +``` + +```javascript +// program using switch statement +let a = 2; + +switch (a) { + + case 1: + a = 'one'; + break; + case 2: + a = 'two'; + break; + default: + a = 'not found'; + break; +} +console.log(`The value is ${a}`); +``` +✅ Test your understanding of this code and the following code by running it in a browser console. Change the values of the varaiable a to change the returned `console.log()`. + + ## Logical Operators and Booleans Decisions might require more than one comparison, and can be strung together with logical operators to produce a Boolean value.