In javascript we have the scopes of variables, we should keep in mind that we should:
1) restrict to the maximum the use of that scope, but what that scope is?
The global scope is where you can have variables loose inside the file as for example:
let minhaVariavel = 'variavel global'
always remember to declare your variables with Let as this is the newest way the language has implemented for some troubleshooting improvements.
we also have the function scope, we have the variables within a function and the same will only be seen within it, this would be the most correct way to work but not the only, ex:
function minhaFuncao() {
let minhaVar = 'variável de função é local'
return minhaVar
}
to solve the problem that in reality is a solution we can return in each function the value and then add all.
const tdHeight = function() {
return "alguma coisa"
}
const tdWeight = function() {
return "mais outra coisa"
}
const result = function(){
return tdHeight() + tdWeight()
}
console.log(result())
another output would be to assign global variables
let tdHeight
let tdWeight
function getHeight() {
tdHeight = "alguma coisa"
}
function getDWeight() {
tdWeight = "mais outra coisa"
}
const result = function(){
return tdHeight + tdWeight
}
getHeight()
getDWeight()
console.log(result())
Hi Giovane, all right? I tried to add a "Return tdHeight" and still can’t access
– Fernando Munhoz
worked, thanks!
– Fernando Munhoz