What will be the output from this code?
function sayHi() {
console.log(name);
console.log(age);
var name = 'Lydia';
let age = 21;
}
sayHi();
A: Lydia and undefined
B: Lydia and ReferenceError
C: ReferenceError and 21
D: undefined and ReferenceError
An interesting question which highlights the difference between the let and var keywords in JavaScript. The key thing here to know is that although the var keyword (don't use it!) is hoisted to the top of the function scope, the value is not assigned to the variable until later on so D is the correct answer as we'll get undefined from the name variable and an error because the age variable is not yet declared.
#javascript #shorts
On this page of the site you can watch the video online I got this with a duration of hours minute second in good quality, which was uploaded by the user Code With Bubb 30 January 2024, share the link with friends and acquaintances, this video has already been watched 1,758 times on youtube and it was liked by 36 viewers. Enjoy your viewing!