But if my condition is for the second, if it already logically excludes the previous answer (which should have been logically excluded in else, if there is an alternative anyway), what exactly is the difference and why use else if / when there would be more, if necessary ?
=> No, however the else statement enforces it if you make a mistake in your "logical exception".
Even if this is a little beyond your question, it’s also worth noting that there is no “else if” construct in JavaScript. When you write:
if (...) { } else if (...) { }
what do you basically do:
if (...) { } else { if (...) { } }
it works because you can either pass any expression after the else keyword. See http://www.ecma-international.org/ecma-262/5.1/#sec-12.5 :-)
Update 1
if (weight <= 130) { fighter = "lightweight"; } else if (weight >= 205) { fighter = "heavyweight"; } else { fighter = "middleweight"; }
which in javascript is equivalent:
if (weight <= 130) { fighter = "lightweight"; } else { if (weight >= 205) { fighter = "heavyweight"; } else { fighter = "middleweight"; } }
Update 2
In the original post, Option 2, you will do:
if (computerChoice <= 0.33){ computerChoice = "rock"; } if (computerChoice >= 0.67){ computerChoice = "scissors"; } else { computerChoice = "paper"; }
If the value of computerChoice is <= 0.33, then the following will happen:
if (0.33 <= 0.33) ... // => computerChoice = "rock" if ("rock" >= 0.67) ... else [THE ELSE PART IS EXECUTED!]
Thus, basically computerChoice will be a "paper" at any time when it should be a "rock". You will have “paper” 67% of the time, and “scissors” - in 33% of cases! This is due to the fact that Javascript does not throw an error when trying to compare values of different types (for example, here a string, "rock" and the number "0.67", instead it tries to convert values of the same type using magic ( http://webreflection.blogspot.be/2010/10/javascript-coercion-demystified.html ) and then gladly say “false!” As a math, I could probably scare you by saying that because beyond these enforcement rules, you can prove in javascript that true = false ...
PS: I just noticed that Kevin Nielsen explained this higher than me. Here you go!