Function Scoped JavaScript Variables ACCESS on a 'NON Strict Mode '

function scoped() {
  var a = 150;
  let b = 300;
  const c = 500;
}

a = 300; // Why is it possible to access a function scoped variable?
b = 600; // Same as above
c = 1000; // Same as above and also why I can change value of constant?

console.log(a); // Return: 300 - Why it works?
console.log(b); // Return: 600 - Why it works?
console.log(c); // Return: 1000 - Why it works?

MY QUESTIONS:

  1. Why it is possible (in a non strict mode) to access function scoped variables? Should this NOT be allowed?

  2. Why I can even change a value of a constant?

  3. How does scope or hoisting work behind the scene in this specific case?

4 thoughts on “Function Scoped JavaScript Variables ACCESS on a 'NON Strict Mode '”

  1. the "var" keyword creates global scope variables not block scope variables in JS. means that "a" inside the function, it is same "a" outside it or (in the global scope).

    the "const,let" keywords create a "block scope" variables, which means the b,c inside the function is not the same b,c outside it. there are two of b,c variables 1- inside the function 2- in the global scope (outside of the function).

    sorry for my English, I hope that clear to you.

    Reply

Leave a Comment