Differences between Ruby and Javascript

So, we just started Javascript and, as my table mates know, I am not a fan so far…almost everything that we’ve done so far in JS, I feel could be done much more simply in Ruby.

Javascript is witchcraft…


The three main differences between JS and Ruby I’ve noticed thus far:

  1. Syntax
  2. Looping
  3. Classes as functions?

Let’s go into these one by one.


Syntax:

The first difference that I noticed between Ruby and JS is how one goes about declaring variables and defining functions.

In Ruby, you can simply set any non-reserve word equal to any value, including null.

In JavaScript, however, the process of declaring something as a variable and actually setting the value of that variable are two separate processes which are generally grouped into one line.

https://scotch.io/tutorials/understanding-hoisting-in-javascript

The process of declaring a value followed by setting it in JS is useful as most recently demonstrated by hoisting. “Hoisting is a mechanism where variables and function declarations are moved to the top of their scope before code execution.” This allows for variables to be available throughout the function regardless of their original global or local scope and makes variables available to us before they are declared.

Creating functions in JavaScript is also quite different. Unlike Ruby, where only methods that had arguments were given parentheses, referring to a function in JavaScript and actually calling on the function is denoted by the presence of parentheses.


Looping:

Looping is significantly simpler in Ruby as compared to JavaScript. Any non-programmer can see the following code and have a basic understanding of what these set of instructions is trying to accomplish:

And here is how we would achieve the same objective in JavaScript:


Classes as Functions

JavaScript allows for two methods by which to create a class. The first is similar to what we’re used to.

Additionally, you can use the constructor function to create a class as well as functions within the class. These are usually denoted by a capital letter so they are easier to read in code.

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS

Comparing it to Ruby, the constructor function takes on the task of creating a class and setting the properties and functions specific to instances of the class using “this” as opposed to “self”.

It is easy to add more complexity to the initial constructor function and also to access properties of an instance as well as utilize different functions and act upon these instances.

You can additionally extend classes and allow for them to inherit functions.

There is a lot of functionality that we’ll be learning about over the next few days.

So I guess JavaScript is …

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.