From b5c64d821eb7243fe5a616b7b91075adb194c287 Mon Sep 17 00:00:00 2001 From: tomkeith Date: Tue, 20 Oct 2020 14:18:11 +0300 Subject: [PATCH] javascript switch statement --- javascript/006-javascript-switch-statement.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 javascript/006-javascript-switch-statement.md diff --git a/javascript/006-javascript-switch-statement.md b/javascript/006-javascript-switch-statement.md new file mode 100644 index 0000000..b95acf9 --- /dev/null +++ b/javascript/006-javascript-switch-statement.md @@ -0,0 +1,35 @@ +--- +id: 006-javascript-switch-statement +title: JavaScript Switch Statement +tags: javascript, es6, beginner, +author: Thomas Mwaka +meta-description: Learn how JavaScript switch statement works +--- + +## JavaScript 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. +This is how it works: + +-The switch expression is evaluated once. +-The value of the expression is compared with the values of each case. +-If there is a match, the associated block of code is executed. +-If there is no match, the default code block is executed. + Example +The getDay() method returns the weekday as a number between 0 and 6. + +(Sunday=0, Monday=1, Tuesday=2 ..) +The break Keyword +When JavaScript reaches a break keyword, it breaks out of the switch block. + +This will stop the execution of inside the block. + +It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway. +The default Keyword +The default keyword specifies the code to run if there is no case match: + +Example +The getDay() method returns the weekday as a number between 0 and 6. + +If today is neither Saturday (6) nor Sunday (0), write a default message. \ No newline at end of file