Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Introducing JavaScript Finishing the Game Control the Flow with Conditional Statements

Tsipporah Christopher
Tsipporah Christopher
1,655 Points

What's the difference between using an 'if' and an 'else if' statement when setting the conditions for the poison?

For example, when I simply use the 'if' statement for the poison it still works and lose points.

if (item.key === 'coin') {
      currentScore =  currentScore + 10;
if (item.key === 'poison') {
      currentScore =  currentScore - 25 ;
  }  

compared to

 if (item.key === 'coin') {
      currentScore =  currentScore + 10;
  } else  if (item.key === 'poison') {
      currentScore =  currentScore - 25 ;
  } 

Thanks!

2 Answers

As far as I can see there is no difference besides just clean code. Normally when you want to check multiple conditions you would use an if/else-if condition check. If only checking one condition you would be safe using just the if statement by itself. Anyone else feel free to give their two cents and correct me if I am wrong. I am still learning.

Tsipporah Christopher
Tsipporah Christopher
1,655 Points

Thanks so much! So it is essentially best practices. Got it!