For some reason, I have a const set, and it works until I put it in a nested function and then in a map function, it works, unless I put it in another if statement.basic example:const weirdVar = 'test'; let items = ['item1', 'item2', 'item3']; removeSrt('item'); function removeSrt(str){ console.log(weirdVar); //works items.map(item => { console.log(weirdVar); //works if(typeof item === 'string'){ console.log(weirdVar); //error: Cannot access before initialization return item.replace(str, ''); } }); } //my temp fix function removeSrt(str){ let weirdVarRef = weirdVar; items.map(item => { if(typeof item === 'string'){ console.log(weirdVarRef); //works return item.replace(str, ''); } }); } anyone know whats going on?
Submitted March 05, 2020 at 03:26AM by SwiftNinjaPro
No comments:
Post a Comment