If I open JS console and write:
let foo;
and after:
let foo = "bar"
console show me (rightly)
Uncaught SyntaxError: Identifier 'foo' has already been declared
Now... sometimes I need to inject my code inside an existing script and I don't have tool to determinate if a let variable is already defined.
I try with this code, but there is evident problem with JS scope and logic.... (comment the code)
let foo; // Gloabl variable empty declare in a code far, far away
console.log(foo); // undefined
console.log(typeof foo === "undefined"); // test that determinate if condition is true
if(typeof foo === "undefined"){
let foo = "test";
console.log(foo); // return "test" this means that foo as a local scope only inside this if...
}
console.log(foo); // return undefined not test!
// and .. if I try to double declaration...
let foo = "bar"; //error!
so... How can I prevent double "let" declaration? / How to determinate if a let var is defined (declared?)
P.S with "var" all working fine!!!