Introduction to JavaScript Variables
20 January, 2023
23
23
7
Contributors
What are variables in JavaScript?
Variables are a key concept in Javascript, and they are used to store data that can be accessed and manipulated throughout a program. Variables can be named, assigned values, declared, and modified. The environment in which variables exist is called the lexical environment. Variables are given an initial value before code is executed in a process called hoisting.
In JavaScript, you can declare a variable using the var
keyword, followed by the name of the variable. For example:
var x;
This declares a variable x
. You can also assign a value to the variable at the same time:
var x = 10;
You can also declare multiple variables at the same time:
var x = 10, y = 20, z = 30;
In addition to var
, JavaScript also has the let
and const
keywords, which you can use to declare variables.
The main difference between var
and let
is that let
is block-scoped, whereas var
is function-scoped. This means that variables declared with let
are only visible within the block in which they are defined, whereas variables declared with var
are visible throughout the entire function in which they are defined. const
is similar to let
, but the value of a const
variable cannot be reassigned.
For example:
if (true) {
let x = 10;
var y = 20;
}
console.log(x); // Uncaught ReferenceError: x is not defined
console.log(y); // 20
In the example above, x
is declared with let
and is only visible within the block of the if
statement. Trying to access x
outside of the block results in an error. y
, on the other hand, is declared with var
and is visible throughout the entire function, so it can be accessed outside of the block.
Global Execution Context
In JavaScript, the global execution context is the default execution context. It is created when the JavaScript runtime starts and is associated with the global object, which is the object that represents the global scope in the JavaScript runtime.
In a web browser, the global object is the window
object, which represents the current browser window. In a Node.js environment, the global object is the global
object.
Variables declared in the global execution context are called global variables. They are properties of the global object and are therefore accessible from anywhere in the JavaScript code. Global variables are not recommended, as they can lead to conflicts and are difficult to manage in larger projects.
For example:
var x = 10;
console.log(window.x); // 10
console.log(global.x); // 10
In the example above, x
is a global variable because it is declared in the global execution context. It is a property of the global object (window
in a browser, global
in Node.js) and is therefore accessible using either window.x
or global.x
.
It is generally recommended to use let
or const
instead of var
for variable declarations, and to use function-level or block-level scoping to limit the visibility of variables to the smallest possible scope. This can help avoid conflicts and make your code easier to manage.
Variable Hoisting in Javascript
Variable hoisting is a concept in Javascript where variables are given an initial value before code is executed. This concept allows developers to use variables before they are declared, but it can create confusion in certain situations. To avoid confusion and errors, developers should always declare their variables at the top of the scope in which they are used.
An example of variable hoisting in Javascript is when a variable is declared after it's used. This is possible because the Javascript engine will look for variable declarations before code is executed, and set a value of undefined
for any variables that are used before they are declared. For example:
console.log(x); // prints undefined
var x = 10;
Another example of variable hoisting is when variables are declared multiple times in the same scope. When this occurs, the last declaration of the variable is the one that is used. For example:
var x = 10;
console.log(x); // prints 10
var x = 20;
console.log(x); // prints 20