Ruby and JavaScript: If Statements

March 25, 2016

Homer Simpson

What are If statements

If statements are widely used in Ruby and JavaScript as conditional statements for control flow. If we look at the above image, we see Homer Simpson have 2 actions. The first action is to say "mmm.. Keep it coming" and the second action is to say "I can't eat this!!!" Which action he takes depend on if food == 'junk food', this means if the food is junk food then he will say "mmm.. Keep it coming" otherwise (else) he'll say "I can't eat this!!!"

In the above example, only junk food will trigger Homer's first action, everything else will trigger his second action. Also there are only 2 conditions but we could easily have 5, 10 or more conditions by adding elsif or else if conditions which we will talk about below.

See If in Action

Overall, the logic behind Ruby If and JavaScript If statements are the same but the syntax has some differences. Let's take a look at the code examples:

                                
                    Ruby                                                                JavaScript
                                        
        Example 1:  if true                                                 Example 1:  if (true)                                         
                        puts "this is true!"                                                { console.log("this is true!"); }
                    end                                                                 
                                        
                                        
        Example 2:  puts "this is true!" if true                            Example 2:  if (true) console.log("this is true!");


        Example 3:  if food == "junk food"                                  Example 3:  if (food === 'junk food') 
                        puts "mmm.. Keep it coming"                                         { console.log("mmm.. Keep it coming"); }
                    elsif food == "steak"                                               else if (food === "steak")                                    
                        puts "mm.. ssssssteak"                                              { console.log("mm.. ssssssteak"); }
                    else                                                                else
                        puts "I can't eat this!!!"                                          { console.log("I can't eat this!!!"); }
                    end   
                                        
                                    food == "junk food" ? "mmm.. Keep it coming" : "I can't eat this!!!"
                            
    Differences:
  • Ruby does not require parenthesis while JavaScript does: if true vs if (true)
  • Ruby allows a short one-line way of writing an if statement (without elsif or else) by putting the result before the if while JavaScript does not allow this syntax
  • Ruby uses elsif while JavaScript uses else if
  • Ruby requires the word "end" to close the if statement while JavaScript uses { } brackets but code will pass without the brackets

The good news is both Ruby and JavaScript can use food == "junk food" ? "mmm.. Keep it coming" : "I can't eat this!!!". This is the ternary operator which reads: condition ? result if true : result if false. The question mark and colon are required as part of the syntax. Learn more about ternary operators here.