Notes for a Ruby programmer learning JavaScript

Numbers vs. Integers and Floats

JavaScript has a type called “Numbers” which encompasses Integers and Floats. At first, this sounds great- you don’t have to worry about including the decimal point when dividing! But I’ve run into those funky computer math problems where .9999999999 doesn’t quite equal 1. Also, the “Numbers” class is a bit of a misnomer. All numbers are 64-bit floating point numbers. It might be better to say Numbers are all Floats and there are no Integers.

Strings are immutable.

I spent half an hour trying to write a “reverse” function for strings that reversed the string in place, until I figured out that strings can’t be changed in place in JavaScript. Make a new string!

No String Concatenation

What is simple in Ruby:

“I am #{first_name} #{last_name}, hailing from #{city}, #{state}.”

becomes a long mess in JavaScript:

"I am " + firstName + " " + lastName + ", hailing from " + city + ", " + state + "."


In Ruby, you can just tell a variable to be itself.

approximation_of_pi = 3.14

In JavaScript, the you must declare a variable with the word “var” before assigning it.

var approxPi = 3.14;

Also, as shown above, Ruby variables are often in snake_case whereas JavaScript conventions favor camelCase.


In Ruby, you’re fine leaving out parenthesis when writing or using methods and you don’t have to explicitly return things- Ruby knows the last line in your method is returned.

def method_name pass_this_into_the_method
# stuff in my method

You can’t leave out the parenthesis in JavaScript, even if you aren’t passing things in. And you have to be explicit in your returns.

(function(x) { 
// do some things
return somethingExplicitly;

Also, referencing a method in JavaScript doesn’t run it. Have to have () after it for it to run. And see all those semicolons? Don’t need those in Ruby. I’m fairly certain you can make almost all JavaScript functions with just some parenthesis, semicolons, and some vars and functions thrown it.

Printing to the console

Ruby is very concise.

puts "I'm in the console now!"

JavaScript is not.

console.log("This is printed to the console, too.");

Luckily, most conditionals are the same.

&&, ||, <, >, <=, >= //these can be JavaScript #or Ruby!

except that JavaScript has triple equals.

Use this for asking about in/equality:

if (firstName !== lastName) {
console.log("That seems pretty common to me");
} else if (firstName === lastName) {
console.log("Who invited Moon Moon?!");

There are no .each or .map loops in JavaScript

You are limited to while() and for(). But that doesn’t mean you can’t write your own.

function each(array, operation) {
for (var i = 0; i < array.length; i++) {
function map(array, operation) {
var mappedArray = [];
for (var i = 0; i < array.length; i++) {
var val = operation(array[i]);

Now, lest we harp on JavaScript too much, it’s what allows us to do really cool things on the Internet.

One clap, two clap, three clap, forty?

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