You can't. As others mentioned, this is called variable shadowing. It completely masks the variables from the outer contexts.
The easiest way to get around this would just be to simply name them differently. It should generally not be too destructive to give the child a different variable name:
function parent() {
const total = 30;
function child() {
const childTotal = 20;
console.log(childTotal);
console.log(total);
}
child();
}
parent();
Another alternative would be to pass the parent value in as an argument to the child, and in the parameters, give it a different name there:
function parent() {
const total = 30;
function child(parentTotal) {
const total = 20;
console.log(total);
console.log(parentTotal);
}
child(total);
}
parent();
Finally, if the value was equivalent to a public static value, you could grab it that way:
function parent() {
function child() {
const total = 20;
console.log(total);
console.log(parent.total);
}
child();
}
parent.total = 30;
parent();
That about sums up all of your options. As said earlier, just changing one of the names is by far the best solution for 99% of the cases.