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();