Two Common Errors When Moving from Ruby to JavaScript

rubyjavascriptdebugging

1. Forgetting that ALL functions must be followed by parentheses when called

The error

my_object.toString

should be

my_object.toString()

Spotting it

If you console.log your function call and it prints the function itself (that is literally function () { /* your actual function implementation */ } or similar) rather than the return value of the function, you’ve probably forgotten parentheses.

2. Forgetting to explicitly return things from functions

The error

function add(a, b) {
  a + b
}

should be

function add(a, b) {
  return a + b
}

Spotting it

If your function is returning 'undefined' when you think it should be returning a value, this may be the cause. It is particularly easy to forget this in callback functions, but they need explicit returns, too.