Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Does switch case use strict comparison?

Switch cases use strict comparison (===). The values must be of the same type to match. A strict comparison can only be true if the operands are of the same type.

Can you do comparisons in a switch statement?

Use the switch statement to execute one of many code blocks based on a variable or expression's value. The switch expression is evaluated once. The comparison value will match either a statement value or trigger a default code block.

Should you use switch statements JavaScript?

The switch statement is a part of JavaScript's "Conditional" Statements, which are used to perform different actions based on different conditions. Use switch to select one of many blocks of code to be executed. This is the perfect solution for long, nested if/else statements.

Does switch case use == or ===?

In answer to your question, it's === .


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...


http://qfox.nl/notes/110 answers your question. (This guy knows a lot about the nitty gritty of JavaScript)

Switches in Javascript use strict type checking (===). So you never have to worry about coercion, which prevents a few wtfjs :). If on the other hand you were counting on coercion, tough luck because you can't force it.


Yes, switch "[uses] the strict comparison, ===".

Source: switch - JavaScript | MDN