JavaScript Vs. Ruby on Rails

Transitioning from Ruby to Javascript was a bit of a rocky road.. so i thought it would be cool to to a blog about them both, compare them & see a few differences and similarities :).

so let compare..

Equals sign.

ruby: ==, =. ruby uses == to compare 
while using the = to assign.
javascript: ===, == , = javascript uses === to compare and also uses the == to compare the difference between these two is === is very accurate and == is leniate when comparing. meaning === checks for types & == does not.javascript example:
a = 10
b = '10'
a === b returns false
a == b returns true
so the === in javascript is equivalent to == in ruby the = in javascript is the same as ruby. (it's used to assign)

&& and || (and & or)

JavaScript uses && and || the same as Ruby.

String Interpolation

this is slightly different Javascript uses ticks (``) in the front and end of a string interpolation and uses ${} to interpolateexample:
`hey, this is a
${Javascript} string interpolation example`
Ruby uses a plain quotation mark ('') in front and end of the string and uses #{} to interpolateexample:
'hey, this is a #{Ruby} string interpolation example'

Although you can still use the a couple of other ways to add strings together like
- .concat,
- the
+ operator, I find the previous example much simpler to use.

self vs. this

Ruby's self is basically the same as JavaScript's this.

Functions Vs. Methods

def (ruby) is equivalent to function() (javascript)ruby example: def ruby
puts "hello"
end
___________________
javascript example:function javascript() {
return "hello"
}

Iteration

.map is the same in ruby as it is in javascript.ruby example:
array.map { |string| string.name }
only difference is the way the iteration is written...javascript example:
array.map(string => string.name)
Map returns a new array with the results.
.each vs forEach
.each in ruby is the same as forEach in javascript. Example... array = [1, 2, 3, 4]ruby example:
array.each {|num| num}
javascript example:
array.forEach(num => num )

Object vs. Hash

Ruby's Hash is the same as JavaScript's Object javascript example:
const cat = {color: “grey”, name: “Spot”, age: “12”};
ruby example:
cat = {color: “grey”, name: “Spot”, age: “12”};
only difference is ruby doesn't need var, const or let to assign a variable a value.

camelCase vs. under_score

we name variables in javascript using camelCase in Ruby we use under_score.ruby example: let javascriptVariable = camelCase javascript example:ruby_variable = under_score 

null vs. nill

Ruby's null is the same as Javascript's nill.

That’s just a few of the many similarities and differences… of JS and ruby hope this was a bit more helpful into conversion if you are converting..

thanks for the read :)

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why to Use Node.js: Pros and Cons of Choosing Node.js for Back-end Development

Narrated by test cases: Redux Action Retry

How to fix non responding task bar icons in windows 11

Deploy an Ember.js Listings App in 3 Steps

PURE COMPONENTS IN REACT

Day 31 | 100 Days Full-Stack Challenge

Using async / await with React

Developing an React App in 3 minutes using CodeSandBox

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hypatia

Hypatia

More from Medium

Comparing and Contrasting Ruby and Javascript

Difference between JS Functions and Ruby Methods

Javascript vs. Ruby: Useful array methods cheatsheet

Ruby != JavaScript?