Switch statement questions

In this link below.

It says the switch statement takes === (strict equality). However, in the answer the strict equality is not used. Has something changed since this part of the course was uploaded?

Also, this here:

function caseInSwitch(val) {
  let answer = "";

I am gonna call it param val , (is that the proper name? )

How do I know that param must be taken to the code below to make the entire function works? This is a bugging issue for me, how do I know when and why include params in the functions ??

switch (val) {

What are you seeing that makes you think that strict equality is not used by a switch statement? Internally, a switch statement still uses strict equality, as far as I know.

The term you are looking for is ‘argument’. You create arguments for whatever data your function requires to do it’s job. In this case, the function needs the val for the switch statement.

switch (val) {
  case "something":
    console.log("val is something");
    break;
  case "something else":
    console.log("val is something else");
    break;
}

Is the same as

if (val === "something") {
    console.log("val is something");
 else if (val === "something else") {
    console.log("val is something else");
}

So, strict equality.

2 Likes

So, is there strict equality on this one?

// Only change code below this line
  switch (val) {
    case 1:
      answer = "alpha";
      break;
    case 2:
      answer = "beta";
      break;
    case 3:
      answer = "gamma";
      break;
    case 4:
      answer = "delta";
      break;
  }
  // Only change code above this line
  return answer;
}
// Change this value to test
caseInSwitch(1);

Yes. Strict equality is used in every single switch statement.

Maybe I need to re-read strict equality.

I was under the impression test was asking to use/ type (===) to solve the problem. Strict equality might be achieved without the use of ===

The switch statement itself uses strict equality when comparing the value to the cases: