Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

JavaScript switch vs if - What's the difference?

Asked by Jiten Miglani Oct 6, 2022 1.1K views 1 answer
Share

About this question

I am developing a game in JavaScript where you start with a user input, stored in the variable "controller". The options for the user consists of starting to start the game or about to learn about the game. I was going to use the following code

if(controller === "start"){
    // Game code sitting here
}
else if(controller === "about"){
    // All about the Game
}
else{
    // Tells the user again to type start to start or about to learn about the game
}

when I realised that I should maybe use switch cases instead...

So, my question is when to use switch cases instead of if statements and which I should use in this case. I would also like to know if you think I should store my game code in a function and call it if the controller is equal to "start" or just have it sitting inside the if statement/switch case as it is atm?

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.