Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript switch statement #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions javascript/006-javascript-switch-statement.md
Original file line number Diff line number Diff line change
@@ -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.