DEV Community

Anthony Mendoza
Anthony Mendoza

Posted on

Syntactic Differences Between Ruby and Javascript

So you're learning Javascript coming from only knowing Ruby or vice-versa. In this post, I'm going to go over some of the syntactic differences that confused me at the beginning of my javascript journey. Before we start I want to mention that I love both languages with ruby's syntax almost being like reading plain English and javascript being so flexible in how it lets you work. With that being said let's begin as any rubyist would know most of our code would be wrapped in a 'def'/ method.


Alt Text
Any method in Ruby needs to start with a 'def' and always end with an 'end' and () as parameters.

Alt Text
Javascript's way of defining a function is a bit different for starters it's much more strict with its syntax while in ruby we don't need any brackets to write our code inside of it but we need and 'end' a good way to think about it is 'end == {}'.

Moving on to another huge difference that may be hard to catch at first sight is template literals while this is not strictly bound to a function it's still a huge difference. In ruby, you can pass in information using "#{}" or '#{}' while in javascript the only way to use a template literal is using a grave accent/backticks ${} you are not given the freedom to choose with javascript so beware.

Another way of writing functions in javascript that was recently introduced in ES6 is arrow functions personally to me these are very compact and easier to write. From my knowledge, there is a couple of differences in functionality so move forward with caution.

Alt Text


Ruby has a couple of different types of variables which you will 1000% use, such as (global, class, and instance variables) but for purposes of comparison, I'm only going to show local variables and how to declare one in ruby.

Alt Text
Ruby's variable declaration is super straight forward give me a name and store the information you need in here, Javascript, on the other hand, likes to give you a couple of choices.

The most common way and oldest way of declaring a variable is using good ol 'var'

Alt Text
var is super basic and a staple of javascript a couple of things to know about var is that you can always change its value and it's on the global variable spectrum once declared it will be hoisted to the top of the page allowing room for some bugs, once again proceed with caution.

Alt Text
"let" is a lot like var but one key difference is unlike var it won't be hoisted to the top of your page (usual safe bet to use).

Alt Text
last but not least is const, one of the most strict. Once assigned if you try to change it you will produce an error saying it has been assigned. When using const use it for values that won't change.

Each loop

One of the last Syntactic differences I will cover for now is the use of .each or forEach.

Alt Text
Ruby's .each loop is almost like reading English it's a very easy way to loop through information the |list| is the parameters to give the loop and once again like most things it ruby you need to give it an "end".

Alt Text
Javascript also makes it's for each loop very easy to read minus the end to finish it off. I hope this helps anyone learning any of these languages to differentiate some the syntax :).

Top comments (2)

wulymammoth profile image
David • Edited

Welcome to, Anthony

You’re gonna learn a lot in this journey!

A couple things I’d like to highlight:

  1. when you say, “like reading plain English”, I think what you’re trying to say is that, “it’s expressive” — as it allows you to express your ideas without too much thought. Ruby has a lot of methods in its standard library and it ends up being a double-edged sword for those coming into Ruby and wondering why there are so many ways to do the same thing
  2. when you simply define a function, it’s just that — a function. It isn’t a method. A method is a function defined on a class or object whose receiver is itself. People mix this up all the time, but if you care about precision aka being pedantic, it’s worth knowing this difference
  3. you took screenshots of your code snippets — you can simply use markdown, just wrap your code using a “fenced code block” (example here), and you can specify the language used after the first three ticks
  4. not sure if “strict” is the right way to describe JavaScript’s syntax. I think of it more as simply different. JavaScript’s syntax is much more common in the outside world than Ruby’s syntax as it has its roots in C — other languages that have similar syntax that you may have heard of are: Java, C++, Go. It’s much more prevalent. In fact, I can’t really think of too many other languages that are similar to Ruby’s. It’s a bit of a stretch, but maybe Python, i mean the beginning reserved word is def, but that’s about it and it has no end
  5. what you’re describing to be “template literals” is known as string interpolation in Ruby. It’s template literals in JavaScript
  6. both Ruby and JavaScript allow you to declare and initialize variables in a single expression
  7. Nice description of const. Are you familiar with the #freeze method in Ruby? It essentially provides the same guarantees
  8. There’s an issue with your use of the loops in both languages. The parameter name that you’re giving to your callback function in JavaScript and your block in Ruby is misleading. You’ve declared and initialized a shipping list full of “items” not a shopping list of “lists”…

In Ruby:

shopping_list = ['apples', 'pears', 'kale']
shopping_list.each { |item| puts item } 
                       👆 # probably shouldn't be "list"

In JS:

const shopping_list = ['apples', 'pears', 'kale'];
shopping_list.forEach(item => console.log(item));

// if we wanna be fancy (this does the same as above in less code)

I find JS having the slight edge here, because the way that it reads is, "for each item in the shopping list, perform the operation I'm passing in"

antman profile image
Anthony Mendoza

Hi David,
Thank you for your comment. It is extremely helpful to get feedback in this programming journey, also I did not know about #freeze completely blew my mind thank you. 🙌🏻