How do I compare two variables containing strings in JavaScript?

=== is not necessary. You know both values are strings so you dont need to compare types.

function do_check()
{
  var str1 = $("#textbox1").val();
  var str2 = $("#textbox2").val();

  if (str1 == str2)
  {
    $(":text").removeClass("incorrect");
    alert("equal");
  }
  else
  {
    $(":text").addClass("incorrect");
    alert("not equal");
  }
}
.incorrect
{
  background: #ff8888;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="textbox1" type="text">
<input id="textbox2" type="text">

<button onclick="do_check()">check</button>

instead of using the == sign, more safer use the === sign when compare, the code that you post is work well

Tags:

Javascript