3
Can anyone explain to me what the problem is with the word 'name' in Javascript? see this code:
the result of the first code will say that the variable name is an object and that name is a string, and the difference(apparently) of the variables is only the naming.
<script>
var name = ["Henrique"];
var nome = ["Henrique"];
document.write("Tipo da variável nome " + typeof(nome));
document.write('</br>');
document.write("Tipo da variável name " + typeof(name));
</script>
already when I place the code inside an IIFE the result will be object for both.
<script>
(function(){
var name = ["Henrique"];
var nome = ["Henrique"];
document.write("Tipo da variável nome " + typeof(nome));
document.write('</br>');
document.write("Tipo da variável name " + typeof(name));
}());
</script>
name
is a reserved Javascript variable. If you test on your console, you will see that it exists even without declaring it, I do not know the need for it. In the example below it works because you are declaring the variablename
in an isolated scope, so it will only exist within your Function() {}block. Basically you will have a name that is in the scope above (in the case of the javascript reserved) and another name within the scope of the anonymous function that you created.– Wallace Magalhães
Wallace, post as answer :) Just not exactly a reserved word, is a global variable available in browsers (
window.name
).– bfavaretto