Learning Javascript As a Rubyist
Tags: javascript, learningtocode, ruby
Published: 2020-01-10 20:47:41

---
title: Learning Javascript As a Rubyist
published: true
date: 2019-09-17 01:37:11 UTC
tags: javascript,learning-to-code,ruby
canonical_url: https://medium.com/@mindyzwanziger/learning-javascript-as-a-rubyist-64ea4d64e969
---

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

![](https://cdn-images-1.medium.com/max/1024/1*FyWmdsQEnvWsCh_fEvZIFA.jpeg)<figcaption>Photo by <a href="https://www.pexels.com/@gratisography?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Gratisography </a>from <a href="https://www.pexels.com/photo/man-person-legs-grass-539/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a></figcaption>

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_](https://launchschool.com/)_) 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:

```ruby
def method_name(parameters)
 # some code
end
```

A Javascript Example:

```javascript
function methodName(parameters) {
 // some code
}
```

I find this quote helpful to further clarify this delineation from [Tiffany White](https://dev.to/tiffany) on a [dev.to article](https://dev.to/tiffany/what-is-the-difference-between-a-function-and-a-method-in-javascript-3mkj) 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:

```ruby
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:

```javascript
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference). I’d recommend starting by looking at something familiar, like the [Array documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array).

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:

```ruby
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:

```javascript
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();`