0

In the following code, why am I not able to update a barcode value dynamically? I want to update the code value using updatecode(str, code) but still getting empty

var barcode;
var shapecode = "";
var typecode = "";

function updatecode(str, code) {
  str = code;
}

function updatebarcode() {
  barcode = shapecode +"-"+ typecode;
  console.log(barcode);
}
updatecode(shapecode, 200);
updatecode(typecode , 200);
updatebarcode();
halfer
  • 19,824
  • 17
  • 99
  • 186
Mona Coder
  • 6,212
  • 18
  • 66
  • 128
  • Does this answer your question? [Pass a string by reference in Javascript](https://stackoverflow.com/questions/1308624/pass-a-string-by-reference-in-javascript) – Klaycon Dec 19 '19 at 22:58

3 Answers3

0

The problem you are running into here is the difference between pass by reference and pass by value.

When you call a javascript function with a primitive (such as a string), it doesn't pass the same variable into the function. Instead it makes a copy of the value of that variable, then passes in a new variable with that value. So when you modify str inside updatecode, it is modifying a variable local to that function, rather than the variable you passed into it.

I'm not 100% sure if you can pass a primitive by reference, but I know you can pass an object by reference, so maybe try this (it's a bit hacky but I'm not totally sure what you're trying to do, so trying to make it work within the context of the code you posted):

var barcode;

var codeObject = {
   shape: '',
   type: ''
};

function updatecode(object, key, code) {
   object[key] = code;
}

function updatebarcode() {
  barcode = codeObject.shape +"-"+ codeObject.type;
  console.log(barcode);
}
updatecode(codeObject, 'shape', 200);
updatecode(codeObject, 'type' , 200);
updatebarcode();
Dexygen
  • 12,287
  • 13
  • 80
  • 147
Dr_Derp
  • 1,185
  • 6
  • 17
  • Actually, strings are passed "by reference" (as much as they can be in js), it's just that they're immutable - and re-assigning the parameter will never change the passed variable regardless of if it's passed by value or reference. – Klaycon Dec 19 '19 at 22:54
  • Ok, good to know. It doesn't really make a difference in the expected result, but good to have the correct reasoning. – Dr_Derp Dec 20 '19 at 23:41
0

You can't pass a value by reference like you want to do. This thread answers well your question: Pass Variables by Reference in Javascript

Mart Coul
  • 490
  • 6
  • 15
  • Strings are passed "by reference" (as much as they can be in js). Even if it was an object, re-assigning the parameter won't accomplish anything or change the passed object - it needs to be a property on the object or some other mutation of the parameter. – Klaycon Dec 19 '19 at 22:55
0

The 'str' variable is a functional-level variable. Modifying it wont effect the external variable. You should do sth like you do to modify barcode. You can do this:

let barcode;
let shapecode = '';
let typecode = '';

function updatecode(str, code) {
  str == 'shapecode' ? (shapecode = code) : (typecode = code);
}

function updatebarcode() {
  barcode = shapecode + '-' + typecode;
  console.log(barcode);
}
updatecode('shapecode', 200);
updatecode('typecode', 200);
updatebarcode(); // logs 200-200;
Yunus Emre
  • 125
  • 7