5

Here in this script cars[i] is used as a condition how is does the program identify weather the condition is true or false and the program stops correctly after it enters the 4th variable in the array ?

So the question is : how the program identifies the condition and how is cars[i] a condition.

var cars = ["BMW", "Volvo", "Saab", "Ford"];
var i = 0;
var text = "";

for (;cars[i];) {
    text += cars[i] + "<br>";
    i++;
}
2
  • 1
    Are you trying to understand the code or asking for help to do something ? Commented Mar 30, 2015 at 6:02
  • I am asking you to help understand the code,its confusing I cant move forward Commented Mar 30, 2015 at 6:03

1 Answer 1

7

Two things are at work here:

  1. In JavaScript, when you use a value as a condition, it's coerced to be a boolean. The values 0, "", NaN, null, and undefined coerce to false (they, plus false, are called "falsey" values); all others ("truthy values") coerce to true.

  2. If you try to access a non-existant entry in an array, you get back undefined.

So when i reaches 4, cars[i] is undefined, which coerces to false, and the loop stops. But relying on that is likely to be setting a trap, because if there are any entries in cars that coerce to false (there aren't in that example, but...), the loop will stop before processing the entire array.


Side note: for (; condition ;) is a very strange way to write while (condition). If you don't have initialize, test, and update parts, for probably isn't the control structure you want. You've said you're trying to understand this code, so I'm guessing you didn't write it; just beware that the quality may not be that high.

Here's the "normal" way to write that loop:

var cars = ["BMW", "Volvo", "Saab", "Ford"];
var i;
var text = "";
for (i = 0; i < cars.length; i++) {
    text += cars[i] + "<br>";
}

Or something a bit more modern:

var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = "";
cars.forEach(function(car) {
    text += car + "<br>";
});

Or something a bit more advanced, but perhaps offputting for beginners:

var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = cars.reduce(function(acc, car) {
    return acc + car + "<br>";
}, "");
Sign up to request clarification or add additional context in comments.

5 Comments

Pointing out that for (; condition ;) is essentially just a while (condition) is probably the most important part. Since i is being both initialized and updated, the code should have been for (var i = 0; condition; i++)
@T.J.Crowder If i="0 then it is a false statement right ?
@JokerSpirit: I don't understand the question.
@T.J.Crowder See you said "",0,NAN are false values right ?here the variable i=0;then the condition is false right. then the loop should stop.
@JokerSpirit: Falsey, not false. Yes, if you did i = 0 and then you used i as the condition, it would be false and the loop would stop. Note that that's not what I'm doing anywhere in the answer, though. The only loop using i in the answer is for (i = 0; i < cars.length; i++). The condition in that is i < cars.length, not i.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.