Aryan PrajapatKnowledge Contributor
What is the output of the below code? function checkAge(age) { if (age < 18) { const message = “Sorry, you’re too young to get your driving license.”; } else { const message = “Yay! You’re are eligible!”; } return message; } console.log(checkAge(21));
What is the output of the below code? function checkAge(age) { if (age < 18) { const message = “Sorry, you’re too young to get your driving license.”; } else { const message = “Yay! You’re are eligible!”; } return message; } console.log(checkAge(21));
Ans: Reference Error
Variables with the const and let keyword are block-scoped. A block is anything between curly brackets ({ }). In this case, the curly brackets of the if/else statements. You cannot reference a variable outside of the block it’s declared in, a Reference Error gets thrown.