Why does using the < operator work? (4/17)


#1

When I run this code:

var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"];
for (var i = 0; i < languages.length; i++) {
console.log(languages[i]);
}

It logs all 5 of the numbers even though I have only used the 'greater than' operator but when I replace '<' with '=<', it gives me "SyntaxError: Unexpected token <". What am I doing wrong?


#2

You get syntax error because there is not such comparison operator as '=<'. How would it read? Equal to or less than? It should be other way round:

>= // Greater than or equal to
<= // Less than or equal to

In your for loop, < operator works because it evaluates to true since languages.length is 5.


#3

@vebra is correct, but in this instance there is no need for the <= syntax since it will read off one too many array elements and display as undefined. The correct answer is actually what you placed in your initial question.

var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"];

for (var i = 0; i < languages.length; i++) {
    console.log(languages[i]);
}