Syntactic Differences Between Ruby and Javascript
Tags: javascript, ruby
Published: 2020-07-10 05:47:03

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*

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vst4mh47h3onk01n6ovi.png)
Any method in Ruby needs to start with a 'def' and always end with an 'end' and () as parameters. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7r5cmflkmuz9corck2yr.png)
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](https://dev-to-uploads.s3.amazonaws.com/i/jlwdelvqizws4kcjnml9.png)

####*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. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fuyq2z0nyihiobkcz5hp.png)
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](https://dev-to-uploads.s3.amazonaws.com/i/h37hkai0ec05jt881pn0.png)
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](https://dev-to-uploads.s3.amazonaws.com/i/gcvyvawsb6g4nfl3d64d.png)
"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](https://dev-to-uploads.s3.amazonaws.com/i/bsqczz1ae8xywwesk57o.png)
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](https://dev-to-uploads.s3.amazonaws.com/i/eyrqeejj57jv7qkq2tki.png)
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](https://dev-to-uploads.s3.amazonaws.com/i/po0vgzgwv60hkypsoumo.png)
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 :).