Jan Dennison

Variations in Variables: JS v Ruby

Feb 4, 2015

Here, let me hold that for you

Variables are staples of many programming languages, including JavaScript and Ruby. Just like we learned in high school algebra, a variable is simply a placeholder representing something else. Let's take a look at the similarities and differences in variables between Ruby and JavaScript.

X = X

In both Ruby and JavaScript, unlike algebra, programming variables can be standins for more than just numbers. Variables can hold strings, arrays, booleans, even whole objects and functions. Both can also take multiple variables declared on one line (although with different structure, and this structure isn't used very often as it's not as readable).


    // JavaScript variables:
    var x = "Jan rocks";
    var y = true;
    var z = 100;
    // or
    var x = "Jan rocks", y = true, z = 100;
  

    # Ruby variables:
    x = "Jan rocks"
    y = true
    z = 100
    # or
    x, y, z = "Jan rocks", true, 100
  

Also in both languages, variables can have either global or local scope, and are accessed simply by calling the variable name.


    // Javascript
    var global = "Anyone can access me!"; // this is a global variable

    function foo() {
        var local = "Only things inside foo() can access me!";
        // this is a local variable
        console.log(local);
    }

    console.log(global);
  

    # Ruby
    $global = "Anyone can access me!"  # this is a global variable

    def foo
      local = "Only things inside .foo can access me!"
      # this is a local variable
      puts local
    end

    puts global
  

Those variable attributes are about the only things the languages have in common. Let's now look at what they do differently.

JS != R

Ruby actually has multiple explicit variable types with differing scopes and a different syntax for each.


    CONSTANT            # accessible by all, doesn't change AT ALL in program
    $global_variable    # accessible by all
    @@class_variable    # shared by all instances of a class
    @instance_variable  # shared within one instance of a class
    local_variable      # shared only within a method
  

Is a Constant still a variable when it is not variable? /deepthoughts

Syntax is also a bit different between the languages. As you can see from the first code example, JavaScript variables are introduced with var, whereas Ruby variables just get right to it.

One last difference is actually debatable: Ruby variables can simply be separated with a new line, whereas the JS variables all end with a semicolon. The debatable part is that JS doesn't always require the semicolon, and some versions will let you get away with leaving it off (or forgetting, if you're in the habit of writing Ruby). However, at least today that practice is not encouraged and most JS writers prefer that you go ahead and use the semicolon regardless.

So there you have it! Hopefully that was helpful to review variables in JS and Ruby and how they compare.