learning-zone / javascript-basics

JavaScript Basics
https://learning-zone.github.io/javascript-basics/
1.75k stars 616 forks source link

Wrong code example #14

Closed wellitongervickas closed 3 years ago

wellitongervickas commented 3 years ago

Q. What is the precedence order between local and global variables?

Current:

var msg = "Good morning";
function greeting() {
  msg = "Good Evening";
  console.log(msg);
}
greeting();

Maybe missed "var" in inner variable:

var msg = "Good morning";
function greeting() {
  var msg = "Good Evening";
  console.log(msg);
}
greeting();
learning-zone commented 3 years ago

This question is about, local variable takes precedence over a global variable with the same name. Here, we are overwriting global variable with local one, which is declared on top. So, this is not an issue.