-1

function runif() {
  let a = document.getElementById('test').value;
  if (a == 1) {
    console.log("works");
  }
}

function runswitch() {
  let a = document.getElementById('test').value;
  switch (a) {
    case 1:
      console.log("working");
      break;

    default:
      break;
  }
}

function runswitchOne() {
  let a = parseInt(document.getElementById('test').value);
  switch (a) {
    case 1:
      console.log("working");
      break;

    default:
      break;
  }
}
<form action="">
  <input type="text" id="test">
  <input type="button" onclick="runif()" value="click to check if">
  <input type="button" onclick="runswitch()" value="click to check without parseInt">
  <input type="button" onclick="runswitchOne()" value="click to check with parseInt">
</form>

This is the form I have created with a text input and two buttons.

In which the if statement recognize the input and does operation

But in switch I have to make it parse to recognize

I do not understand why it works? I know text input gives sting but if so how if() statement works without parsing?

normally we use if(a == "1") to compare string and not if(a==1)?

but even so it works

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • 3
    `=` is assignment, not comparison, your `if(a = 1)` does not actually test anything, it will always be fulfilled – CertainPerformance Feb 09 '19 at 07:36
  • Probably a duplicate of https://stackoverflow.com/questions/1029781/what-is-exactly-the-meaning-of-in-javascript/1029792#1029792 – Taplar Feb 09 '19 at 07:44
  • 1
    Equality check `a == b` does, if types of a and b are different, *type coercion* (conversion) for you. Identity check `a === b` does also compare types and only returns true if both value and type match. – connexo Feb 09 '19 at 07:49
  • if ("one" == "one") compares string and if ("one" == one ) throws error or not??? – Jackson Jegatheesan Feb 09 '19 at 07:54
  • let even if 1 is considerd as "1" ; will it compare as if ("1"==1) --> how will it be?? – Jackson Jegatheesan Feb 09 '19 at 07:56

2 Answers2

3

The switch case does the strict comparison (check both value and type).

The value of the element is of type string. Inside switch case value is of type int and does not match. Thus without conversion, you code does not work.

But when use a == 1 only the value are checked not the type and "1" == 1 evaluates to true. If you do strict comparison (e.g., ===), "1" === 1 evaluates to false because in this case though the value is equal but the type is not equal.

The following will work:

switch (a) {
    case "1":
         .....
Mamun
  • 66,969
  • 9
  • 47
  • 59
2

You can make it work without parsing, just change your expected value in the switch (1) to a string ("1"):

switch (a) {
    case "1":
        //Rest of code
}

Of course, in an if statement when you're using ==, this does type conversion for you (1 == "1"). In a switch, it behaves like a === (equality operator, does not perform type coercion).

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • 1
    so you are saying that in an if statement when i am using ==, this does type conversion ! ..,yea, i tried it worked no body ever taut this i was thinking it just compares tqsm! – Jackson Jegatheesan Feb 09 '19 at 08:04
  • 1
    No problem @JacksonJegatheesan. Using `===` is also useful, because you learn when you're using different types in the wrong way. – Jack Bashford Feb 09 '19 at 08:05