JS Switch Statement

In a case of multiple IF/Else/Else If statements, the switch statement can be used to clean the code up a bit.

The switch statement takes a value, then evaluates the value. If a match is found, it executes the condition.

  case ERROR:
      console.log('Error occurred")
  case WARN:
      console.log('Warning issued')
  case DEBUG:
      console.log('Debug mode')
Code language: PHP (php)

If the value for codeOut matches ERROR, then it throws the console log output “Error occurred.” Likewise, if the value is the string WARN it would output ‘Warning issued,” and so on.

About Author /

Leave a Comment

Your email address will not be published.

Start typing and press Enter to search