switch (t.value) {
case < 5:
alert('hi');
break;
}
I know it’s the part where I have “< 5”. How do I make it so that it has a case where t.value is less than 5??
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
An
ifstatement seems best suited for this purpose, but although I do not recommend it the fact that JavaScript will let you switch on any datatype (and not just numbers/enums like some languages) means you can do this:The above should select whichever case matches first, noting that several if not all of the cases could be true.
In a way that style is more readable than a long series of
if/else if, but I wouldn’t use it in a team development environment where it could confuse other developers.Another, more conventional use of
switchfor your less than 5 scenario would be as follows (assuming you know the range thatt.valuecould possibly be):