I’m doing a switch statement in javascript:
switch($tp_type){
case 'ITP':
$('#indv_tp_id').val(data);
break;
case 'CRP'||'COO'||'FOU':
$('#jurd_tp_id').val(data);
break;
}
But I think it doesn’t work if I use OR operator. How do I properly do this in javascript?
If I choose ITP,I get ITP. But if I choose either COO, FOU OR CRP I always get the first one which is CRP. Please help, thanks!
You should re-write it like this:
You can see it documented in the
switchreference. The behavior of consecutivecasestatements withoutbreaks in between (called “fall-through”) is described there:As for why your version only works for the first item (
CRP), it’s simply because the expression'CRP'||'COO'||'FOU'evaluates to'CRP'(since non-empty strings evaluate totruein Boolean context). So thatcasestatement is equivalent to justcase 'CRP':once evaluated.