JavaScript!

Ruby's slightly verbose uncle

May 2, 2015

Uncle Rico forever

JavaScript

Last year I tried to learn JavaScript. I'll be honest, creating variables and if/else statements seemed straight forward. Once I got to the for loops, though. Wow, wow, wow.

for (var i = 0; i < something.length; i ++){
   do stuff
}

What the crap is this?? THIS looks like real code, not human language but computer language. This year, however, I realized that JavaScript is kind of like Ruby's wordy(verbose) uncle. It's been around for a while, pretty much does similar things as Ruby, maybe takes a little longer to tell a story, but still knows how to do really cool things. ;)

What is JavaScript

Really quickly, for people who are new to programming, JavaScript is a programming language that allows you to add computer scripts to web pages. So, if HTML/CSS allows you to specify where the words and pictures go, and what color the fonts and background need to be, JavaScript allows you to add animations. JavaScript also allows the user to interact with the page. When you click a button, or enter text into a form field, there are events that fire up and calculations or decisions that are made with regards to your input. JavaScript allows you to write logic that handles those interactions.

Variables

Just like Ruby, JavaScript uses variables to store values. The only difference is that in JS, you need to actually say it's a variable. So in Ruby:

dinner = spaghetti

whereas in JS

var dinner = spaghetti;

Syntax

JavaScript has a slightly more verbose syntax than Ruby. You have to use a lot of semicolons. Sometimes you don't have to, but when in doubt, put a semicolon anywhere and everywhere. Also, lots of squigly {} brackets and regular () brackets to display where things begin and end. In the variable example above, I had to put a semicolon after the variable declaration. Here's a few more examples. In Ruby,

puts "this is simple"

def this_is_a_method
   do stuff
end

in JS,

console.log("so many semicolons");

var name = function(x){
   do stuff;
};

The For Loop

This is the thing that tripped me up last year but it's basically like a Ruby while loop, but does all the counting for you in one line. I like the for loop now. In Ruby,

count = 0
while count < 5
   do stuff
   count += 1
end

in JS,

for (var i = 0; i < 5; i ++){
   do stuff;
};



They both basically do the same thing, but JS does in slightly less lines.

Object Literal

Object literal in JS is pretty dang cool. It's basically like a hash, but in JSON format, which is so incredibly easy to read and interact with. So in Ruby,

hash = {
   "dinner" => pizza,
   "breakfast" => eggs
}

in JS,

var object = {
   dinner : ramen,
   breakfast : waffles,
};

In addition, using the JS object literal, you can gain access to the values inside of a particular property using the dot notation.

object.dinner
#=> ramen


So convenient!

Functions

Functions are sort of like methods in Ruby. They are objects that hold a bunch of behavior together. Here is a method in Ruby:

def dinner
   puts "what's for dinner?"
   d = gets.chomp
   if d == "ramen"
    puts "great choice!"
   end
end


in JS,

function = dinner(d){
   console.log("what's for dinner?");
   if (d == "ramen"){
    console.log("great choice!");
   };
};

dinner("ramen");


Conclusion

As you can see, Ruby and JavaScript are very similar. There are some things that Ruby does better and others that JavaScript does better, but when you are learning programming, you need to know both because JavaScript is the language of the web. :)



Previous Next