0

consider this:

function something() {
    let a = { "a": "val", "b": [{ "c": "val" }] };
    console.log(a);
    a.b = a.b.map(f => ({ "test": "something" }));
}

In the above, I expect the console.log to be executed before the map function and so any mapping/transformation done by the map function shouldn't affect the value of console.log but it's not the case.

Map function modifies the value of the console.log even if it is called before map function. Here is what is logged:

enter image description here

so b actually got changed by map! however if I comment the map function, it works fine. A bit confused here. does map hoists it's changes or something fundament is that what I am missing here?

Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
beNerd
  • 3,314
  • 6
  • 54
  • 92
  • Hint: that blue `i` at the top right hand side is a important clue... Hover over it and tell us what it says. – Gerardo Furtado Mar 19 '18 at 01:39
  • @GerardoFurtado it says value below evaluated just now...what does it exactly mean? – beNerd Mar 19 '18 at 01:41
  • It means exactly that: the value in the console **is not** the value when you actually called it. It has been changed after that (by the `map`). So, *"map function modifies the value of the console.log even if it is called before map function"* is in fact the expected behaviour. I won't close this as duplicate because I can't find a good reference, but this one is close, please read it: https://stackoverflow.com/questions/7389069/how-can-i-change-the-default-behavior-of-console-log-in-safari – Gerardo Furtado Mar 19 '18 at 01:42
  • @DanielA.White just found a better one... – Gerardo Furtado Mar 19 '18 at 01:45

0 Answers0