Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Why not use ternary operators/switch statements instead of if-else-if-...-else statements?
Can anyone please explain when should you use a switch, ternary operators, and, if-else-if-...-else statements? In which case one works better than other?
Steven Parker225,730 Points
A ternary would normally be used to select a value, where a conditional statement ("if") would be a good choice for controlling statement execution.
A switch statement is good for conditional execution when there are a number of cases that all depend on a single value, where an "if...else if" chain would work better when the cases depend on different things.