JavaScript is a funny language, if you ask me. Maybe because we have only done things with Ruby here at Dev Bootcamp, and approaching new language is always weird. I don't know. But what I have seen in this short time of using JavaScript, is a feeling that it is much more complicated than Ruby. I do not know enough to really judge it, but I just go by my gut feeling. I'll compare the two today, and we'll see the differences in loops between both languages.
While loop:
In JavaScript:
var x = 0;
while (x < 9){
console.log(x)
++x
}
In Ruby:
x = 0
while x < 9
puts x
x += 1
end
Output is the same for both:
Here we can see that both are fairly similar. JavaScript uses curly brackets, which are easy to miss, but in ruby you can't use a really cool counter like in JS. As well, one uses puts
, other console.log()
, but apart from that, they are very close.
Do - While loop:
What if we want to execute the code at least once, even though the condition fails. There's a very good way of doing that in JavaScript, but if we want to do the same thing in Ruby, we need to get a bit creative, and use an "if statement" to break out of the loop.
In JavaScript:
var x = 0;
do {
console.log(x);
++x
} while (x < 1)
In Ruby:
x = 0
loop do
puts x
x += 1
break if x >= 1
end
Output is the same for both:
This means that it executes the code once and than it exits the loop. We can see the main difference between this two is the condition we set at the end. This is what computer looks at when it wonders if he needs to continue. In case of JavaScript, we set it to false
( x < 1
), and in case of Ruby it's the other way around. We set it to true
( x >= 1
).
There's one more.
For loop:
When writing in JavaScript text editors will often offered you a shortcut for this loop. In Sublime Text 3, the editor I use, you only need to type for
and computer will offer me the option to write the loop for me. The one integrated in Sublime is made for arrays, but you can always modify it as you wish. Let's see what our two languages offer us for for
:
In JavaScript:
for (var i = 0; i < 9; i++) {
console.log(i)
}
In Ruby:
for i in 0..8
puts i
end
Output is the same for both:
JS has a loop with counter. A very similar thing we did with 'while' loop example. We set up to what number we want it to count, and it will. Once condition is false
, loop breaks. Very similar thing happens with Ruby, just that a lot of it happens "under the hood". Here i
will take the role of number in range we just set. Like in our case from 0 to 8. But we could easily replace the range (0..8) with an array. I'll add a second example for Ruby's for
. This time with an array of strings.
array = ["a","b","c","d","e","f","g","h"]
for i in array
puts i
end
Output:
It's a very useful loop when dealing with arrays, for a simple fact that it always has the index number for you. In our examples we could use i
as an index, and we'd be able to pick up, delete, switch, increase, or do whatever we want to the element that would pass our hypothetical "if statement".