Javascript Switch statement not working as expected with string
<p>I'm trying to use the following switch statement in an ajax success callback:</p>

<pre><code>success: function(datain) {
                    switch (datain)
                    {
                        case "ERROR. No ID. Try again":
                            $(".errors").append('There was an error.');
                            break;

                        case "ERROR. Wrong captcha. Try again":
                            $(".errors").append('There was an error.');
                            break;
                    }
                }
</code></pre>

<p>datain is a string (<code>typeof datain</code> returns <code>string</code>) and it does indeed contain the same text, capitalization and punctuation as the case so why would it not match either of the cases? </p>

<p><code>console.log(datain)</code> and <code>console.log("ERROR. No ID. Try again")</code> match exactly and both return a <code>typeof</code> of <code>string</code> so why does my case never get matched?</p>

<hr>

<p><strong>Solution and cause</strong></p>

<p>The solution is offered by palswim below <code>$.trim(datain)</code>. The cause was visible in Firebug and it was the fact that the string had a newline at the end while my switch case did not... so I was getting "foo\n" and trying to match "foo". Given that js uses <code>===</code> in the switch this, naturally (now that I see it), is why it failed.</p>
