0
function formatNum(num) {
  if (num >10) {
    num = 10;
    console.log(num + " >10");
  }
};
formatNum(number_var);

If I try to use above function to determine whether a number surpasses a certain amount, it notices that it is indeed above that value, but won't overwrite it with 10.

The weird thing is that the console does print out 10 > 10, which makes it seem like it did change the value. Referencing the value later on still shows the value 200, for example. If I replace the part where I call the function like this:

if (number_var >10) {
  number_var = 10;
}

This way does work, which makes me think I'm missing something like a function not able to change a variable's value.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Note that there's no such thing as a 'jQuery function'. As there's no jQuery in this question at all I've removed that tag for you. – Rory McCrossan Apr 20 '20 at 08:22

4 Answers4

0

The issue is because JS passes integer arguments by value, not by reference. One way to work around this to meet your goal is to update the argument in the function and return it to the caller, like this:

function formatNum(num) {
  if (num > 10) {
    num = 10;
  }
  return num;
};

let number_var = formatNum(200); // = 10
console.log(number_var);

However it's worth noting that the formatNum() function is redundant and can just be replaced with a call to Math.min():

let number_var = Math.min(10, 200); // = 10
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

You only change the var num inside the function, not the one on the outside.

you need something like this:

function formatNum(num) {
  if (num > 10) {
    num = 10;
    console.log(num + " >10");
    return num;
  }
};

var number_var = 100;
console.log(number_var); // here it is still 100 
number_var = formatNum(number_var);
console.log(number_var); // here it is 10
cloned
  • 6,346
  • 4
  • 26
  • 38
0

I would generally go for the reassignment method. However, there is another option you could use. You can wrap your value inside an object.

function formatNum(num) {
  if (num.value > 10) {
    num.value = 10;
  }
};

const number_val = { value: 100 };
console.log(number_val);
formatNum(number_val);
console.log(number_val);

This passes the object reference by value, but you can mutate the object however you like. Changes will be reflected in the object passed. Note that replacing num with a new object (num = { value: 10 }) will not change the provided parameter. You have to mutate the passed object.

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
  • `This passes the object reference by value` I think you mean it 'passes the object *by reference*' – Rory McCrossan Apr 20 '20 at 08:50
  • @RoryMcCrossan JavaScript passes everything by value. That's why you can't use `num = 10` or `num = { value: 10 }`. `num` is passed by value, so reassigning it wont be reflected outside the function. In the scenario of the object the referenced is passed by value. JavaScript uses a copy of the object reference as function parameter. So the reference itself is passed by value. – 3limin4t0r Apr 20 '20 at 08:57
  • Thanks - you're right, [ref](https://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference). I never knew that about JS. I did know the data type model was bad, but not *that* bad :) – Rory McCrossan Apr 20 '20 at 08:59
  • This also applies for JavaScript: [Is Ruby pass-by-reference or pass-by-value?](https://robertheaton.com/2014/07/22/is-ruby-pass-by-reference-or-pass-by-value/) – 3limin4t0r Apr 20 '20 at 09:00
0

JavaScript engine creates a new execution context when you call the function like yours formatNum and your num variable created inside that context as a property.

So there is no reference between number_var and num becuase number_var is part of global execution context.

you could achieve this by using execution context scope chain

 function formatNum() {
  if (number_var >10) {
    number_var = 10;
    console.log(number_var + " >10");
  }
};
var number_var =200;
console.log(number_var)
formatNum();
console.log(number_var)
Rajesh
  • 193
  • 2
  • 13