The Basics: Javascript VS Ruby
In this blog I will be breaking down some of the major differences between Javascript and Ruby. If your journey is similar to mine you learned Ruby first. If this is the case hopefully this helps you understand Javascript better. For those who learned Javascript first I hope you gain a better understanding of Ruby.
Variable Declaration
In Ruby you can simply take a variable and set it equal to the value you want to give it. Within saying that, you also want to make sure you have the proper syntax with what you are setting the variable to. With Javascript you have to set it using let, var or const. Here is an example below:
What is the difference between let, const and var?
let
and const
are block scoped and Var
is function scoped. Var
is accessible in the function where it is defined. Var
initially will return undefined and can bleed into other parts of your function so it is best to just stick to let
or const
. If you create a variable using let
or const
they are only accessible within the curly { }
brackets. They are also only available within the block they are declared in.
There are also differences between let
and const
as well. Const
is non-immutable, meaning the variable can only be assigned once, while let
can be reassigned. Best practice when assigning variable is to use let
or const
because var
is prone to cause a lot of bugs. It’s still important to know what var
is because there are websites that were built before ES6 which is before the creation of let
and const
. To avoid issues with functionality, we as developers must learn about var
and how its uses within the code. Here is an article going over the benefits are let
and const
: https://levelup.gitconnected.com/stop-using-var-to-declare-variables-in-javascript-6c0caec16f43
Defining Functions
Creating functions in Javascript and Ruby are different as well. In Javascript to create a function you use the keyword function
and in Ruby you use the keyword def
. Javascript also uses curly braces to show the contents that are within function, while ruby indicates the beginning with def
and the end with the the end
keyword.
Implicit Returns
In Ruby there are implicit returns while Javascript does not have implicit returns when we are creating functions. If you want your function to return something you will need to write the implicate word return.
Programming Language
I think it’s important to understand the uses of both languages. Ruby is a backend language while Javascript is a front-end language. When it comes to Javascript, it is a front-end language used for client side application, server-side , browser level interaction and changes. These are the origins of both languages however, they can cross paths.
Falsey values
We have to mention falsey values and the differences there. Nil
and undefined
in Ruby are false. In Javascript there are more falsey values. Examples of falsey values in JavaScript are 0
, 0n
, null
, undefined
, false
, NaN
, and the empty string "".
Data types
Ruby and Javascript have a few data types in common. Those are : numbers, booleans, strings ands symbols. Data types in Ruby that are different from Javascript are arrays, hashes. Javascript data types that are different from Ruby are objects, null and undefined.
Elsif vs Else if
This one is my favorite. When making conditional statements and Javascript or Ruby, they both have the if
conditional. The second conditional is what sets them apart. Javascript you use else if
while Ruby uses elsif
. Remember: there is no e in elsif
! I made that mistake before.
There are so many things you will learn as you continue to learn either language. I used to hate Javascript, I won't lie. But I have learned to love it because of all the great things I am able to do with it! I hope that this blog has brought you some clarity in your coding journey. Here are more references to blogs that talk about the differences :
I hope these references below and this blog help you better understand the differences between the two languages. Happy coding !
Ruby vs JavaScript: Compared and Contrasted https://careerkarma.com/blog/ruby-vs-javascript/#:~:text=In%20short%2C%20Ruby%20is%20an,one%20languages%20on%20the%20web.
JavaScript vs Ruby — https://www.educba.com/javascript-vs-ruby/
Ruby vs JavaScript as a first programming language https://medium.com/constructorlabs/ruby-vs-javascript-as-a-first-programming-language-2f2c944629fe
Here is also more blogs on javascript objects:
JavaScript Objects Quickly Explained — https://medium.com/front-end-weekly/javascript-objects-quickly-explained-b078f906254a
Everything About Javascript Objects https://towardsdatascience.com/everything-about-javascript-object-part-1-854025d71fea