How do I use comparison operators in a switch?

  • 0
    How to use & lt; = in a switch?
    Here's my example:
    switch (элемент.value.length) {
    
      case >=5:
        // code
    }

    In general, how to substitute & gt; = for 5?
    JavaScript Anonymous, Apr 24, 2019

  • 2 Answers
  • 0
    You shouldn't do this in switch, it's for exact values. You should use logical operators in if - this is more correct and much faster. Well, so
    const num = 10;

    switch (true) {
    case (num <= 5):
    console.log('Num меньше или равна 5')
    break;
    case (num >= 5):
    console.log('Num больше или равно 5')
    break;
    default:
    console.log('Num не известно')
    }
    // Num больше или равно 5


    Shit code! Do not repeat!
    Levi Murillo

  • 0
    No way. case takes a ready-made expression, not a part of it, in which case "& gt; = 5" is missing an operand on the left



    Use "if" to compare other than "=="
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!