DEV Community

Dai
Dai

Posted on

Global scope and Script scope

Global scope and Script scope

let a = 0; // script scope
var b = 0; // global scope
function c() {} // global scope
Enter fullscreen mode Exit fullscreen mode

Global scope
var and function are contained by the window object. It means global scope.
That's why no need to write like below. Global object(window) can omit to write.

console.log(window.b);
window.c();

// We can omit "window"
console.log(b);
c()
Enter fullscreen mode Exit fullscreen mode

Script scope
When we declare variables with let or const.
Those are gonna be script scope.
Generally, script scope is also called global scope, because script scope is similar to ease of use with global scope.
But to be exact it's different.

let a = 0;
const b = 0;
Enter fullscreen mode Exit fullscreen mode

Image description

Latest comments (0)