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