Is It Safe to Assume Strict Comparison in a JavaScript Switch Statement

Is it safe to assume strict comparison in a JavaScript switch statement?

Take a look at ECMA 262, section 12.11, the second algorithm, 4.c.

c. If input is equal to clauseSelector as defined by the === operator, then...

Does the switch statement equal to === or ==?

It only uses strict comparison. In particular, it never falls back to type coercion even when no strict matches are found — it will immediately skip to the default clause, if any. From MDN:

The program first looks for a case clause whose expression evaluates to the same value as the input expression (using strict comparison, ===) and then transfers control to that clause, executing the associated statements. If no matching case clause is found, the program looks for the optional default clause...

javascript switch/case : are types compared?

Yes, types are compared.

If input is equal to clauseSelector as defined by the === operator,
then set found to true.

ECMA-262, page 95.

Strange behaviour of js switch statement evaluating true/false

Although I am not sure, the reason seems to be that switch does not perform implicit coercion. Boolean() might as well be considered explicit, as it was written to accept values other than booleans. Again, I'm not sure, but a quick fiddle might confirm the suspicion.

JavaScript switch statement fails to match case

The value is in string format. Convert it to number because switch statements do not coerce types (unlike if statements)

switch(+input.value) {

OR

switch(parseInt(input.value, 10)) {

I'll also suggest to use array or object.

var arr = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];

result.innerHTML = arr[+input.value] || 'Not a Digit';

Javascript switch wrong case

.val() returns a string, not a number. So after the line:

activiteitn = $("#m2activiteitn").val();

the variable activiteitn actually equals "1", not 1. Since case...switch uses strict equality, your test will always fail.

To fix, simply change the above line to:

activiteitn = +$("#m2activiteitn").val(); // convert to a number

or

activiteitn = parseInt($("#m2activiteitn").val(),10); // convert to an integer

or

activiteitn = parseFloat($("#m2activiteitn").val()); // convert to a float

JavaScript: using a condition in switch case

This works:

switch (true) {
case liCount == 0:
setLayoutState('start');
var api = $('#UploadList').data('jsp');
api.reinitialise();
break;
case liCount<=5 && liCount>0:
setLayoutState('upload1Row');
var api = $('#UploadList').data('jsp');
api.reinitialise();
break;
case liCount<=10 && liCount>5:
setLayoutState('upload2Rows');
var api = $('#UploadList').data('jsp');
api.reinitialise();
break;
case liCount>10:
var api = $('#UploadList').data('jsp');
api.reinitialise();
break;
}

The only thing necessary is switch(true){...} and for your case expressions to evaluate to booleans.

It works because, the value we give to the switch is used as the basis to compare against. Consequently, the case expressions, also evaluating to booleans will determine which case is run. Could also turn this around, and pass switch(false){..} and have the desired expressions evaluate to false instead of true.. but personally prefer dealing with conditions that evaluate to truthyness. However, it does work too, so worth keeping in mind to understand what it is doing.

Eg: if liCount is 3, the first comparison is true === (liCount == 0), meaning the first case is false. The switch then moves on to the next case true === (liCount<=5 && liCount>0). This expression evaluates to true, meaning this case is run, and terminates at the break. I've added parentheses here to make it clearer, but they are optional, depending on the complexity of your expression.

It's pretty simple, and a neat way (if it fits with what you are trying to do) of handling a long series of conditions, where perhaps a long series of ìf() ... else if() ... else if () ... might introduce a lot of visual noise or fragility.

Use with caution, because it is a non-standard pattern, despite being valid code.

JS Switch case not working correctly always default is executed

Your discselect is coming across as a string..so you need to change your case to:

switch ( discselect ){
case '1':
break;
case '2':
break;
case '3':
break;
default:
alert(discselect);
}
}


Related Topics



Leave a reply



Submit