DEV Community

Mindy Zwanziger
Mindy Zwanziger

Posted on • Originally published at Medium on

Learning Javascript As a Rubyist

A handful of simplified comparisons to make those first few steps into Javascript a bit easier.

Photo by Gratisography from Pexels

I love Ruby.

I also recently discovered that some people vehemently dislike it.

So here I am, learning Javascript.

The people I talked to would probably be appalled that their comments had such an effect on me. Here’s a secret —I still love Ruby and Javascript is the next part of the curriculum at the online school I’m attending (Launch School) so I’d be studying it anyway, but that makes for a much less interesting story.

So here I am, learning Javascript.

The first few days of diving into a new language feels a little like a firehouse of information being directed at your face. As a former high school teacher, I’m here to tell you how to deal, with a tip — we learn best by connecting new information to old information.

So let’s make some connections!

Writing Syntax

Commenting

In Ruby, we use # to make one-line comments, and =begin with =end for our multi-line comments.

A Ruby Example:

# Comments are great!

=begin
Honestly, I find this a weird
syntax for comments. 
=end

To write a comment in Javascript, you use // for a one-liner, and /* with */ for a multi-line comment.

A Javascript Example:

// I'm a wonderfully useful comment!

/* 
Me
Too! 
*/

cAsE

In Ruby, we use snake_case for variables, files, directories, etc…, CamelCase for class names, and SCREAMING_SNAKE for constants.

Javascript mostly uses camelCase. You’ll see PascalCase with constructors (more about whatever those are soon), and SCREAMING_SNAKE for constants, just like in Ruby. Underscores (_) are for use in constants only.

Methods vs. Functions

In Ruby, we use the term “method” to denote a function. In Javascript, we see the term “function” more often, as it’s part of the method/function definition.

A Ruby Example:

def method_name(parameters)
 # some code
end

A Javascript Example:

function methodName(parameters) {
 // some code
}

I find this quote helpful to further clarify this delineation from Tiffany White on a dev.to article earlier this year:

In short: a method is a function that belongs to a class. In JavaScript, however, a method is a function that belongs to an object.

String Interpolation

Both languages provide syntax for inserting variable values into a string.

A Ruby Example:

site_name = "Medium"

"Welcome to #{site_name}"

Javascript uses backticks (`) and a dollar symbol ($) to create what is called a “Template String Literal” which allows for string interpolation.

A Javascript Example:

var siteName = 'Medium';

`Welcome to ${siteName}!`

Methods, Etc…

A great reference point for these is the Mozilla Developer Network (MDN). Here’s the Javascript Reference link. I’d recommend starting by looking at something familiar, like the Array documentation.

Let’s start with something familiar — instance methods!

Instance method

Luckily for us, both Ruby and Javascript use instance methods.

A Ruby example: [4, 3, 2].sort

A Javascript example: [4, 3, 2].sort // Oh hey, they're the same — NICE.

In our handy MDN reference list, You can tell if a method is an instance method if it includes the wordprototype.

Static method

These are akin to Ruby’s class methods.

A Ruby example: Hash.new
A Javascript example: String.fromCharCode(65, 66 ,67) // Returns 'ABC'

Constructor

As an introductory point of reference, constructors can be compared to Ruby’s classes. While Javascript constructors and Ruby classes are very different at the core, they are both the vehicles by which a new object is created, and in that, they are similar.

A Ruby example:

class Dog
 def initialize(name, age, breed)
 @name = name
 @age = age
 @breed = breed
 end
end

new_dog_object = Dog.new('Coco', 8, 'Mixed')

A Javascript example:

function Dog(name, age, breed) {
 this.name = name;
 this.age = age;
 this.breed = breed;
}

newDogObject = new Dog('Coco', 8, 'Mixed');

Other comparisons can be made, of course, but this will give you a great place to start! Code well, my friends!

article.stop();

Top comments (0)