The above answers are not correct. The console.log() will run before the loop finishes and that is why you get undefiend.
You can find your answer here.
you have to think sync like this piece of code:
function delay() {
return new Promise(resolve => setTimeout(resolve, 300));
}
async function delayedLog(item) {
// notice that we can await a function
// that returns a promise
await delay();
console.log(item);
}
async function processArray(array) {
for (const item of array) {
await delayedLog(item);
}
console.log('Done!');
}
processArray([1, 2, 3]);
this will give you 1,2,3,done which means the console.log is happening at the end of loop!
var newStr = '';undefined + 'qwe'==='undefinedqwe'