I tried using this solution but it didn’t work for me. In my case I am trying to save a variable using 1 function and call it from another
JavaScript
x
13
13
1
var postalcode = "code didn't change";
2
3
export function save_postal_code(code) {
4
var localcode = code
5
let postalcode = localcode;
6
console.log(code);
7
}
8
9
export function get_postal_code() {
10
console.log(postalcode);
11
return postalcode;
12
}
13
The save_postal_code function logs the correct value, but the get_postal_code function doesn’t. I don’t know what I am doing wrong.
Advertisement
Answer
You’re redeclaring postalcode
inside save_postal_code()
instead of updating its value.
The code needs further revision, but that’s outside the scope of this answer.
To have postalcode
updated inside save_postal_code()
, try:
JavaScript
1
13
13
1
var postalcode = "code didn't change";
2
3
function save_postal_code(code) {
4
let localcode = code
5
postalcode = localcode;
6
}
7
8
function get_postal_code() {
9
return postalcode;
10
}
11
12
save_postal_code("123")
13
console.log(get_postal_code())