A Tale of Two Languages: A Comparison of Ruby and JavaScript

Emily Bragg
5 min readJul 27, 2022

--

Learning multiple coding languages is essential to a successful career as a full-stack software engineer. Becoming familiar with both frontend and backend development helps to increase your knowledge as a developer, which in turn can provide more opportunity. However, creating and maintaining knowledge regarding many coding languages is no easy feat. I recently transitioned from working with JavaScript and React to Ruby and Sinatra, building an application that involves the two. This process made me realize that there are many similarities, differences, and equivalences between these languages, and drawing upon both can help make the transition between the two (and others!) much easier.

Similarities: The Commonalities of Ruby and JS

Despite their differences, Ruby and JS have a number of commonalities, both in theory and syntax. First, both are object-oriented languages — meaning they operate using objects (a bundle of data and logic) to create customized functions. Both also have truthy and falsy values representing whether code is true or false. Additionally, they both use symbols (though, it is worth noting they are represented differently, and are more common in Ruby). In regard to arrays, both can store any type of data in an array; and in regard to functions, both can pass in values as arguments to different methods, as well as provide default arguments. Finally, strings can be defined in both using ‘single quotations’ or “double quotations”.

Differences: The Disparities of Ruby and JS

Despite their commonalities and similar instances of syntax, Ruby and JS are ultimately two different coding languages. With this in mind, there are a number of differences in use, syntax, and methods that are important to identify.

First, JS and other programs that use JS, such as React, are used for front-end development on the client side. Ruby, and its applications that require it such as Rails or Sinatra, is used for backend development for the server side. Applications created in JS run in the browser, while applications in Ruby run in the terminal. Variables in JS are written using camelCase (newVariable), while variables in Ruby are written using snake_case (new_variable). Further, when creating a new variable, those in JS do not need to be assigned a value right away, while those in Ruby do. Additionally, for variables with no value, JS will return null or undefined, whereas Ruby only returns nil. Any error messages given in JS are typically ambiguous and leave it to the developer to figure out; while error messages in Ruby typically give more information and direction in regard to the error. Ruby errors indicate the type of error, as well as file, line of code, and scope in which the error occured. For example, a syntax error within Ruby might look like this:

Whereas a JavaScript syntax error, as pictured below, is much shorter, and does not provide us the in-depth information Ruby does to assist with tackling the mistake.

Lastly, JS also requires an explicit return (return statement), while Ruby has an implicit, or automatic return.

Equivalences: The Parallels of Ruby and JS

There are many aspects of Ruby and JS that are considered equivalent to one another as well. For example, a hash in Ruby is the equivalent of an object in JS. For commenting out code, using # in Ruby is the same as // in JS. Both also have ways of displaying and debugging code. To print something to the console, using puts in Ruby is the same as console.login JS. To further evaluate code, Ruby users can enter IRB (interactive Ruby) using irb, while JS users can use the browser console directly. Finally, for debugging, Ruby uses binding.pry, while JS simply usesdebug — however, both accomplish the same thing by establishing a breakpoint in code to be directly evaluated.

Finally, functions — or methods, as they are referred to in Ruby — can be written to accomplish the same thing, though their syntax looks quite a bit different. For example, if we wanted to write a function that returns the sum of two numbers it would look like this in JS:

Achieving this same value would look like the code snippet below in Ruby:

This example highlights some of the things highlighted in the previous sections, such as the use of snake_case versus camelCase and implicit versus explicit return. However, other differences arise that are worth noting as well. The Ruby method is initialized using the def keyword, and ends with the end keyword, whereas JS functions are initialized using the function keyword, and end with a closing curly brace. This example also highlights the differences in how each language prints something, as mentioned above (console.log versus puts).

While this list is not all-inclusive, these examples cover many of the main similarities, differences, and equivalences of Ruby and JS. Whether you are starting your career as a software engineer or continuing your education in the field, being able to identify the connections between languages is not only useful in the transition of learning one language to the next but helps to reinforce that knowledge as time goes on.

--

--

Emily Bragg

Full-stack developer, yogi, and Taylor Swift enthusiast - here to share my knowledge, successes, and failures of my career journey in tech