I tried this in jade served by express but got "unexpected identifier" as an error.
- switch(myvar)
- case: "0"
span First Case
break
- case: "2"
span Second Case
break
- case: "3"
span Third Case
break
- case: "4"
span Fourth Case
break
I was curious as to what is the syntax for a switch statement, if there is one.
Update: Jade, not express.
A typical syntax involves: the first select , followed by an expression which is often referred to as the control expression or control variable of the switch statement. subsequent lines defining the actual cases (the values), with corresponding sequences of statements for execution when a match occurs.
The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type. You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
1) The expression used in switch must be integral type ( int, char and enum). Any other type of expression is not allowed.
Switch case statements follow a selection-control mechanism and allow a value to change control of execution. They are a substitute for long if statements that compare a variable to several integral values. The switch statement is a multiway branch statement.
EDIT
This question is apparently about Jade instead.
But the answer is still yes :).
But it's called case
:
From the docs
case friends
when 0
p you have no friends
when 1
p you have a friend
default
p you have #{friends} friends
Javascript has a switch statement.
switch(variable){
case 1:
// do something
break;
case 2:
// do something else
break;
// and so forth
default:
// do something if nothing
break;
}
Being that Express.js runs in Node.js which is just JavaScript -- yes. Express has a switch
statement since JavaScript has a switch
statement. (Even coffeescript has a switch
that "compiles" down to a JavaScript switch
statement.)
MDN reference: switch
statement
It looks like your syntax is messed up there -- what are those "-" characters? You're also missing the :
from the end of each case
statement, and you're not break
ing after each case which means the code for ALL cases will ALWAYS run regardless of the condition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With