Iterating through over an array and printing each element
Ruby:
my_array = ["first_element", "second_element", "third_element"]
my_array.each do |element|
puts element
end
JavaScript:
const myArray = ["firstElement", "secondElement", "thirdElement"];
myArray.forEach((element) => {
console.log(element);
});
Iterating through over an object and printing each key/value pair
Ruby:
person = {
name: "John",
last_name: "Doe",
age: 35,
height: 178
}
person.each do |key, value|
puts "#{key}: #{value}"
end
JavaScript:
const person = {
name: "John",
lastName: "Doe",
age: 35,
height: 178
};
Object.keys(person).forEach((key) => {
const value = person[key];
console.log(`${key}: ${value}`);
});
Else/If statements
Ruby:
age = 15
if age > 17
puts "You are an adult"
elsif age > 12
puts "You are a teenager"
else
puts "You are neither a teenager nor an adult"
end
JavaScript:
const age = 15;
if (age > 17) {
console.log("You are an adult");
} else if (age > 12) {
console.log("You are a teenager");
} else {
console.log("You are neither a teenager nor an adult");
}