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.
methods
Any method in Ruby needs to start with a 'def' and always end with an 'end' and () as parameters.
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.
Variables
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.
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'
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.
"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).
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.
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".
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)
Welcome to Dev.to, Anthony
You’re gonna learn a lot in this journey!
A couple things I’d like to highlight:
def
, but that’s about it and it has noend
const
. Are you familiar with the#freeze
method in Ruby? It essentially provides the same guaranteesIn Ruby:
In JS:
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"
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. 🙌🏻