user2323036
user2323036

Reputation: 1585

Mysterious working of a Javascript - forth value returns no exception when array defined for three

The below code works fine in javascript

var items = [1,2,5]
for (var i = 0, item; item = items[i]; i++) {
  console.log(item = items[i]);
}

OUTPUT : 1 2 5
  1. I am not sure how this program works successfully

  2. The forth item is undefined and what will happen in the last iteration.

  3. Can we able to create a similar function in Java ?

  4. items[3] at the end how it will be handled in the above function?

Upvotes: 1

Views: 69

Answers (3)

niksvp
niksvp

Reputation: 5563

The code works fine as you have assignment in the for loop condition check

During the first iteration item is undefined but it executes the loop without termination as you assign the first value of array to item and it is no more undefined

Try it with comparision operator as item == items[i], you will find your loop no more executes even once.

When it comes outside of array i.e. items[3] which returns undefined and thus temp goes undefined and terminates the loop as in javascript undefined, null, 0 and false all are same.

This function wont work in Java as it would complain that it requires condition not the assignment in for loop.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1075139

Two key things to start with:

First, in JavaScript, whenever you try to retrieve the value of an object property that doesn't exist (including an array entry), the result of the retrieval is the value undefined.

Second, when you use a value like a flag, as in an if statement or the "test" part of a for (or any other loop), etc., JavaScript with "coerce" whatever value you used to be boolean. [This is different from (say) Java, which requires that you only use boolean variables as that sort of flag.] Because JavaScript does this and it comes up a lot, we use the terms "truthy" (coerces to true) and "falsey" (coerces to false) to categorize values. The "falsey" values are 0, "", NaN, null, undefined, and of course false; all other values are truthy.

Okay, armed with that information, let's look at your loop:

The forth item is undefined and what will happen in the last iteration.

There are only three iterations. A for statement is made up of three expressions separated with ;, and a loop body. The expressions are the initialization, the test, and the increment:

//          /--------------------------------initialization
//          |               /--------------- test
//          |               |           /--- increment
//   vvvvvvvvvvvvvvv  vvvvvvvvvvvvvvv  vvv
for (var i = 0, item; item = items[i]; i++) {

The loop is evaluated like this:

  1. Do the initialization
  2. Do the test
  3. If the test was false, terminate
  4. Do the body
  5. Do the increment
  6. Go back to step 2

The "test" part of your for loop is item = items[i], which is an assignment. The value of an assignment expression is the value being assigned. So when i is 3, that assignment is item = items[3] which is effectively item = undefined. That assignment has the value undefined, which is a falsey value, so the body of the loop isn't run a fourth time.

Can we able to create a similar function in Java ?

Yes, it looks almost identical other than that with Java, you use a type with the variables, and you can't use (just) the item = items[i] thing for the "test" part of the for (for the same reason you get an exception below).

items[3] at the end how it will be handled in the above function?

You don't have any function in your question. After the loop, in JavaScript, accessing items[3] would give you undefined (see above). In Java, it result in an ArrayIndexOutOfBoundsException.


Side note: The = in this line is an assignment, not a comparison:

console.log(item = items[i]);

If you meant to compare them, do this:

console.log(item == items[i]);

...which will give you the output

true
true
true

Upvotes: 3

Teemu
Teemu

Reputation: 23406

Here is what happens in your loop:

1. i = 0; item = 1; i = 1;
2. item = 2; i = 2;
3. item = 5; i = 3;
4. item = undefined; --> breaks the loop

Notice, that if you'd have items = [0,1,2], your loop would never be executed.

Upvotes: 2

Related Questions