Blueprint of a switch statement

Switch Statement; A Basic JavaScript Concept

Often people say to code newbies; learn HTML, CSS, JAVASCRIPT. What they always (conveniently) forget to mention Is that JavaScript is like a maze; not straightforward, complex.

There is good news though, in the form of the Basic JavaScript course on Freecodecamp. It offers a map, and a rope to ease the stress of navigating through JavaScript. Many JavaScript concepts were broken down; The Switch Statement is one of them.

What does the JavaScript Switch Statement mean?

Switch is the cooler twin of the ‘if/else’ statement. Just like it sounds, the if/else statement is used with conditional logic. If the condition is right, the program executes a statement, if the condition isn’t right another statement is executed.

To explain the code above; the statement in the bracket is the condition that has to be true or not for the ‘return statements’ to be executed. So, if num is greater than 10, JavaScript should return “Bigger than 10”, else(i.e. if num is ≤ 10) JavaScript should return “10 or less”.

Now let’s talk about the cooler twin; Switch statement. The Switch statement provides functionality like that of the ‘if/else’ statement, It carries out the function of the ‘if/else’ statement in a more efficient way (talk about being cool). The Switch statement first evaluates its expression. It then looks for the first ‘case value’ whose expression matches the value of the result of the input expression. The Switch statement can have many case statements which define various possible values, statements are executed from the first matched case value until a break is encountered.

Case values are strictly tested i.e. (===) is used. The ‘break’ after console.log tells JavaScript to stop executing statements once the statement of the case that matches the expression has been executed, in the absence of the break statement, JavaScript will execute the statements till it gets to the end of the code.

Points to note about the switch statement;

  1. The switch statement executes once and the statement associated with that particular case is executed.
  2. The switch statement stops executing immediately it finds a case that satisfies the condition.
  3. If there is no matching case value, JavaScript looks for ‘default’ at the end of the code and executes it’s statement. In the absence of ‘default’, the next block of code is executed.

As a final point, I’d like to point out that the ‘if/else’ statement isn’t useless the Switch statement just makes code look cleaner, especially when conditions are more than two.

So, here ends that tale of the Switch Statement. Ciao

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

filterMap in JavaScript — Solving Performance issues with .filter() and .map() used in conjunction

Usecases of JAVA SCRIPT

PARCEL DEVELOPER DOCUMENTATION

Angular 8 Series — What and Why?

JavaScript Basics You Need to Understand-1

useResponsive: Writing Your Own React Hook for Handling Responsive Display Sizes

Stop using "Else” & start with "Branchless” statement in programming

Applying and Organizing Styles in React Native

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abiona Quadrat Adewemimo

Abiona Quadrat Adewemimo

More from Medium

Hoisting in JavaScript

Map(), Filter(), Every(), Some(), Reduce() are your best friends!

Map(), Filter(), Every(), Some(), Reduce() are your best friends!

Top JavaScript Skills Earnest for Developers to Include in Their Arsenal

Understanding CSS Selectors