
Why Everyone Loves to Hate JavaScript
February 12, 2016
I have been coding for: 7 weeks

I was apprehensive about getting started with JavaScript this week. Here's what I was hearing:
"JavaScript is a miserable programming language. Anyone who tells you differently has advanced JavaScript stockholm syndrome."
"JavaScript goes to such enormous lengths to avoid crashing that it impedes its usability."
"It sucks."
Since JavaScript really isn't going anywhere, we might as well suck it up and learn it! In this first week, it helped to directly compare how something is done in Ruby versus JavaScript. Let's explore what key-value pair structures look like in both languages, using my favorite Chicago restaurants for different meals.
Ruby: fave_restaurants_chicago = { brunch: "The Bongo Room", sushi: "Rollapalooza", thai: "Andy's Thai Kitchen", pizza: "Coalfire", burgers: "DMK", ramen: "Strings", italian: "Balena" }
JavaScript: var faveRestaurantsChicago = { brunch: "The Bongo Room", sushi: "Rollapalooza", thai: "Andy's Thai Kitchen", pizza: "Coalfire", burgers: "DMK", ramen: "Strings", italian: "Balena" };




The following table will outline the differences in manipulating, accessing, and naming these structures.
Ruby | JavaScript | |
---|---|---|
name | hash | object |
inside curly brackets | key-value pairs | properties |
syntax* | fave_restaurants_chicago {cusine: "name"} | faveRestaurantsChicago {cuisine: "name"}; |
access value | fave_restaurants_chicago[:burgers] => "DMK" | faveRestaurantsChicago.burgers => "DMK" |
add to | fave_restaurants_chicago[:tacos] = "Taco Joint" | faveRestaurantsChicago.tacos = "Taco Joint" |
delete | fave_restaurants_chicago.delete(brunch) | delete faveRestaurantsChicago.brunch |
iterate | fave_restaurants_chicago.each do |key, value| puts value end | for (var key in faveRestaurantsChicago) {console.log(faveRestaurantsChicago[key])}; |
call methods | fave_restaurants_chicago.method | faveRestaurantsChicago.method() |
*Ruby: if keys are NOT symbols, you use hash rocket notation {cuisine => "name"}.
Bottom Line
The data structures slightly differ in the two languages. Getting used to the for loops in JavaScript has been challenging since I grew to love the #each method. One of the biggest differences I've noticed is accessing a value in JS using dot notation, which looks like a method call, but is just calling the associated property. JS methods seem to always have empty parentheses after them, which call the method, and curly brackets must be nested correctly. Cross checking my syntax with resources very carefully in the beginning will help solidify these new conventions and set me on my way toward not hating JavaScript.