0

Possible Duplicate:
Is Chrome's JavaScript console lazy about evaluating arrays?

I write next js function:

function popFromArray(arr){
    console.info(arr);
    console.info(arr.pop());
    console.info(arr);
}

When I write next code in console I expected next output:

>>> popFromArray([12, 432, 52, 523]);
[12, 432, 52, 523]
523
[12, 432, 52]

But I found strange behaviour for me (chrome and firefox):

>>> q = [12, 432, 52, 523];

>>> popFromArray(q);
[12, 432, 52, 523]
523
[12, 432, 52, 523]

>>> popFromArray(q);
[12, 432, 52]
523
[12, 432, 52]

>>> popFromArray(q);
[12, 432]
52
[12, 432]

Is anybody can explain why it happens?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
tbicr
  • 24,790
  • 12
  • 81
  • 106

2 Answers2

2

Try to step over through the function code: it will report properly.

I think it's just a concurrency issue. Console output generation is done in parallel with program execution so result is unpredictable.

Dmitry Nogin
  • 3,670
  • 1
  • 19
  • 35
0

This is the intended behavior of pop(), according to the MDN pop() will:

Removes the last element from an array and returns that element.

To realize your intention, you should try

function popFromArray(arr){
    console.info(arr);
    console.info(arr[arr.length -1]);
    console.info(arr);
}
steveyang
  • 9,178
  • 8
  • 54
  • 80