This forum is now read-only. Please use our new forums! Go to forums
Logical Operators in Switch Statements
Hi,
I tried looking in the web for this but could not find it for front end related languages. But apparently in back end languages like php and I guess python, we can use logical, boolean and comparison operators in switch statements.
Is this true in javascript as well?
Can we use logical, boolean and comparison operators in the switch statements and the ‘case’ parts of the switch statements?
Thanks in advance.
Answer 50e70ba6e734b524700071e9
4 votes
Yes indeed, we can use all three types of operators within switch statements. Here is a quick example I just made:
var number = prompt("Type in any number!");
switch(true) {
case (number < 50):
console.log("case in which number is less than 50");
break;
default:
console.log("case in which number is not less than 50");
break;
}
:-)
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
Thank you so much that works fine, so the matter is when we need to use logical operator in case we must set the switch(true) not the switch (variable). Thanks a lot