switch(value) Last updated: 17. Jul 2023
A JavaScript switch statement is a control flow construct that allows code to be executed conditionally based on the match between a provided expression's value and specific case values, often used as a more readable alternative to a series of if-else statements.
Parameters
Name | Type | Description |
---|---|---|
value | expression | Value/expression to evaluate in the following case statements. |
Returns
null
Example
Code example (JS)
JS is normal JavaScript either running in the browser or on the Docly™ server.let value = 'banana';
switch (value) {
case 'apple':
write('This is an apple');
break;
case 'banana':
write('This is a banana');
break;
default:
write('Unknown fruit');
}
Output
The JS code above produces the output shown below:This is a banana