EN
JavaScript - what is scope?
17 points
Scope determines the accessibility of variables. In JavaScript there are available few scopes.
xxxxxxxxxx
1
var a = 123;
2
var b = true;
3
4
console.log(a); // 123
5
console.log(b); // true
Note: there is alternative way to create and access global variables:
- NodeJS JavaScript:xxxxxxxxxx
1global.c = 'global variable';
2
3console.log(c);
4console.log(global.c);
- WebBrowser JavaScript:
xxxxxxxxxx
1<html>
2<body>
3<script>
4
5window.c = 'global variable';
6
7console.log(c);
8console.log(window.c);
9
10</script>
11</body>
12</html>
xxxxxxxxxx
1
var a = 123;
2
3
function logNumber() {
4
var a = 'text';
5
6
console.log(a);
7
}
8
9
logNumber(); // text
10
console.log(a); // 123
That scope works with let
and const
variables (it was introduced in ECMAScript 6 (ES6/ES2015)).
xxxxxxxxxx
1
let a = 1;
2
3
{ // this brackets create new scope for let keyword variables
4
let a = 2;
5
6
console.log(a); // 2
7
}
8
9
console.log(a); // 1