2023-05-15 - JavaScript


main topics


Ruby vs JavaScript

action Ruby JavaScript
check type var.class typeof(var)
cast to int num.to_i Number.parseInt(42, 10)
name convention lower_snake_case lowerCamelCase
falsy values nil, false false, undefined, null, 0, "", NaN

Hash vs Object

# ruby
my_hash = { "name" => "bob", "age" => 42 }
// js
const myObj = { name: 'bob', age: 42 }

Interpolation:

# ruby
full_name = "#{first_name} #{last_name}"
// js
// known as "Template Literals" in docs
const fullName = ${firstName} ${lastName}

CRUD in arrays:

# ruby

fruits = []

fruits << "Orange"   # Create
fruits.push("Apple") # Create (also valid)

fruits[0]            # Read

fruits[0] = "Banana" # Update

fruits.delete_at(0)  # Delete
// js

const fruits = [];

fruits.push("Apple"); // Create

fruits[0];            // Read

fruits[0] = "Banana"; // Update

fruits.splice(0, 1);  // Delete (1 item at index 0)