⏎ Return

Ruby Hashes vs. JavaScript Objects

Ruby hashes are a collection of unique keys and their values. Ruby hashes enumerate their values in the order that the corresponding keys are inserted. JavaScript objects are almost everything in JavaScript. It is written as name: value pairs. Ruby hashes and JavaScript objects both have very similar syntaxes. The values in both Ruby hashes and JavaScript objects can be any type of numbers, strings, arrays, even hashes or objects.

Ruby Hash example: person = { name: “Kristal”, age: 23, gender: “female” }

JavaScript Object example: var person = { name: “Kristal”, age: 23, gender: “female };

As you can see these are both very similar there are just some slight differences in the syntax. The way you access these values are also different but similar. For example how to access a ruby hash you would do person[:age] … to access a JavaScript object you would do person.age; See how accessing values are similar as well. One important difference that JavaScript has over Ruby Hashes is that in JavaScript object, it can take a function as a value. You can't do that in Ruby because methods aren’t objects and you can’t assign methods to a hash value.