Skip to content

Hey everyone! πŸ‘‹ I'm diving headfirst into a 100-day JavaScript adventure, and I couldn't be more thrilled to share it with you all! πŸŽ‰ Over the next three months, I'll be immersing myself in everything JavaScript has to offer, from the very basics to some seriously advanced concepts.

Notifications You must be signed in to change notification settings

lassiecoder/100daysofjs

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 

Repository files navigation

The "switch" statement

A switch statement is a concise way to compare a value against multiple variants. It replaces multiple if checks and provides a more descriptive way to handle such comparisons.


Syntax

switch(x) {
  case 'value1':  
    ...
    [break]

  case 'value2':  
    ...
    [break]

  default:
    ...
    [break]
}

Here,

  1. The value of x is compared with each case value for strict equality.
  2. If a match is found, the corresponding code block executes until a break statement is encountered.
  3. If no match is found, the code block within the default case (if present) executes.

For example:

let a = 2 + 2;

switch (a) {
  case 3:
    alert( 'Too small' );
    break;
  case 4:
    alert( 'Exactly!' );
    break;
  case 5:
    alert( 'Too big' );
    break;
  default:
    alert( "I don't know such values" );
}

In the above example, a is compared to each case value. Since a is 4, the code block associated with case 4 executes, resulting in an alert displaying "Exactly!"

About

Hey everyone! πŸ‘‹ I'm diving headfirst into a 100-day JavaScript adventure, and I couldn't be more thrilled to share it with you all! πŸŽ‰ Over the next three months, I'll be immersing myself in everything JavaScript has to offer, from the very basics to some seriously advanced concepts.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published