Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is file scope in javascript

What is variable declared within file scope in javascript?
Is there anything file scope, considering multiple files are used in an app.

like image 932
c g Avatar asked May 09 '26 22:05

c g


2 Answers

ES6 modules form their own file scope (as if the entire contents of the file were wrapped in a function).

Variables declared in a module are completely inaccessible from outside that module (unless they're exported).

like image 198
SLaks Avatar answered May 11 '26 10:05

SLaks


In JavaScript, there are only 3 types of scope:

  1. Global Scope (i.e. every variable/function defined outside functions in a file or multiple files)
  2. Functional Scope (i.e. every variable/function defined within the function)
  3. Closure Scope (i.e. code block/function having access to its surrounding lexical scope)
like image 36
Navneet Goel Avatar answered May 11 '26 11:05

Navneet Goel