What will be the result of the following code?

console.log(a);
var a = "a";
function b(){
    console.log(a);
    var a = "a1";
    console.log(a);
}
b();

@basimdev32 ;)

2018 Feb 4, 5:42:40 PM

I forgot that functions also hoisted, that's why second log will clouser the undefined value of a.

2018 Jan 23, 2:13:34 PM

@irina8petrova49 Thanks! Fixed)

2017 May 17, 11:48:05 PM

undefined undefined a1 - it's right answer without 'use strict' with 'use strict' - Uncaught SyntaxError: Unexpected identifier and stopping the script execution

2017 Apr 2, 10:33:32 AM

Isnt var a already global when the first console.log gets executed ? How is it undefined ?

2016 Nov 29, 5:35:05 PM

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes