1

Does JavaScript come with a way to keep track of a variable's identity when it's passed into a function? For example:

var dog = 0;

function dogStuff(animal){
  animal = animal++;
}

In this example I would like to make dog == 1 by passing dog into the function:

dogStuff(dog);

such that

console.log(dog);

would print 1. People have marked this as duplicate to a couple of other questions, but those are a bit too complex for me, a beginner, to understand. I need some responses that assume I have little to no knowledge of JS.

Christoph
  • 21
  • 4
  • You can't do that with primitives. What you *can* do is something like: `dog = dogStuff(dog);` – Nir Alfasi Apr 25 '18 at 22:40
  • Also note that `==` is a boolean operation. – ninesalt Apr 25 '18 at 22:43
  • Use `window.dog++` inside `dogStuff()` function to get what you want – kay27 Apr 25 '18 at 22:43
  • Possible duplicate of [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – ASDFGerte Apr 25 '18 at 22:45
  • Possible duplicate of [Javascript by reference vs. by value](https://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value) – Nir Alfasi Apr 25 '18 at 22:46

2 Answers2

1

First things first: the line animal = animal++ won't do anything because it is a postfix increment. Either just do animal++ or ++animal to increment it.


Nope, dog will not be changed. JavaScript passes primitives by value (Thanks @ASDFGerte for the correction).

var dog = 0;
function dogStuff(animal) {
  animal++;
}
dogStuff(dog);
console.log(dog);  // prints 0

What you want to do (probably) is something similar to what @alfasin mentioned: return the updated value of dog.

var dog = 0;
function dogStuff(animal) {
  animal++;
  return animal;
}
dog = dogStuff(dog);
console.log(dog);  // prints 1

However, if you pass an object and reassign its properties, the original object will be modified (almost like pass by reference):

var dog = { age: 0 };
function incrementAge(animal) {
  animal.age++;
}
incrementAge(dog);
console.log(dog.age);  // prints 1

edit: If you want to assign multiple variables on return, one possible way to do it is to return an array of variables, which can then be assigned with deconstructed assignment:

var dog = 0;
var cat = 52;
function incrementTwoAnimals(animal1, animal2) {
  animal1++;
  animal2++;
  return [animal1, animal2];
}
[dog, cat] = incrementTwoAnimals(dog, cat);  // deconstructed assignment
console.log(dog, cat);  // prints 1, 53
Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
  • Hmm what do you mean by "reassign its properties?" – Christoph Apr 25 '18 at 22:42
  • @ASDFGerte Oops, I meant say pass by value. Fixed – Jonathan Lam Apr 25 '18 at 22:45
  • Note that it's "primitives are passed by value", objects are a different story again, sometimes described as "pass by copy of reference" – ASDFGerte Apr 25 '18 at 22:47
  • @ JonathanLam Thanks! What if I need a function with multiple parameters and my program has multiple variables, like dogStuff(animal1, animal2) and variables dog, cat. If in a particular case the function only updates the value of one of the two variables, how do I tell the computer which variable to change with return. Like if dogStuff(cat,dog) only does animal1++ and not animal 2++, how do I get it to update cat with return? – Christoph Apr 25 '18 at 23:14
  • @Christoph I'm glad this helped! I'll add an edit about ways to do multiple assignment with return – Jonathan Lam Apr 25 '18 at 23:51
1

The function you posted did not return any value that you can use outside of it. So you need to add: return. This means that when you run dogStuff(...) it will actually return a value. And then you save that value in a variable, which can be the same dog that you passed as a parameter.

Here's the full code:

var dog = 0;

function dogStuff(animal){
   return animal++;
}

dog = dogStuff(dog);
Victoria Ruiz
  • 4,913
  • 3
  • 23
  • 40