Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript switch/case : are types compared? [duplicate]

Tags:

javascript

Possible Duplicate:
Is it safe to assume strict comparison in a Javascript switch statement?

Does a switch/case statement in javascript compare types or only values?

In other words, when I have the following code:

switch (variable)
{
    case "0": [...] break;
    case "1": [...] break;
    default: [...] break;
}

is it equivalent to

if ( variable == "0" )
{
    [...]
}
else if ( variable == "1" )
{
    [...]
}
else
{
    [...]
}

or to

if ( variable === "0" )
{
    [...]
}
else if ( variable === "1" )
{
    [...]
}
else
{
    [...]
}

edit: is there a way to force compare values and types at once?

like image 376
iliaden Avatar asked Jun 13 '12 16:06

iliaden


1 Answers

Yes, types are compared.

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

ECMA-262, page 95.

like image 81
Oleg V. Volkov Avatar answered Oct 26 '22 11:10

Oleg V. Volkov