0

I made this object in javascript.

let list = {
   value: 1,
   next: {
      value: 2,
      next: {
         value: 3,
         next: null,
      },
   },
};

And also a function to traverse it...

function traverse(list_) {
   while (list_) {
      console.log(list_.value);
      list_ = list_.next; // (*)
   }
}
traverse(list);

Inside of the function I am changing the reference to the passed argument at (*), so at the end of the function I was expecting the value of list object to be null as well, but when I log the value of list after the function, it still shows the same object...

Does this mean that list was passed by value, and not by reference?

  • Yes, JavaScript does not have pass by reference. Please read the *second* answer on the linked duplicate (https://stackoverflow.com/a/5314911/707111) as the top one is seriously misleading. – Ry- Feb 21 '18 at 17:42
  • Thanks Ryan! That helped me! – Abhishek Mehandiratta Feb 21 '18 at 17:47
  • Glad to hear it! To make a link to other knowledge: if you’ve come across any of Java, Python, Lua, or Ruby, or will in the future – it works the same way in those languages. – Ry- Feb 21 '18 at 17:52

0 Answers0