Crus4

logo

JavaScript Variables – var, let, const


What are JavaScript Variables?

JavaScript is a dynamic and flexible programming language used for developing web applications. One of the most important concepts in JavaScript is variables.

A variable is a container that stores a value, which can be of any data type, such as a number, string, or object. Variables in JavaScript are dynamic, which means you can reassign them to different data types at any time during program execution.

JavaScript Variables – var, let, const

JavaScript Variables can be categorized into three types: var, let, and const. Each type has different characteristics that affect how the variable behaves in the program.

  1. var

The var keyword was the original way of declaring variables in JavaScript. Variables that are declared with var are global-scoped, which means they are accessible within the function they are declared in and also anywhere in the program. If a variable is declared with var inside a function, it is also accessible outside of the function. Variables declared with var are global variable and are accessible from anywhere in the program.

Example 1

<script> 
if (1 == 1) {
    var a = 7;
}
document.write(a);
</script>

In an above example, you have noticed that variable “a” is declared inside the block (curly braces). When we print it outside the curly braces it gives us an expected output. As I already told you variables declared with var are Global-Scoped, that means we can access them from anywhere in the program. If we declare the same variable with let or const we will get an error.

2. let

The let keyword was introduced in ES6 and is a block-scoped variable. A block is a group of statements that are enclosed in curly braces {}. Variables declared with let are only accessible within the block they are declared in, including nested blocks. If you declare a with let inside any block you can’t access it from outside the block.

Example 2

<script> 
if (1 == 1) {
    let a = 7;
}
document.write(a);
</script>

In an Example 2 we have declare the variable “a” inside the block (curly braces). When we try to access it from outside the block, we get an error. As I already told you variables declared with let are block-scoped they are only accessible inside the function they are declared in.

3. const

The const keyword is also introduced in ES6 and declares a constant variable. The value of a const variable cannot be changed once it is assigned. A const variable is also block-scoped and can only be accessed within the block it is declared in.

Example 4

<script>
if (true) {
  const b = 30;
  document.write(b); // Output: 30
}
document.write(b); // Error: b is not defined
</script>

Also remember that the value of const cannot be changed.

Example 5

const a = 10;
a = 20; // Error: Assignment to constant variable

Share This Post!

JavaScript Variables – var, let, const

2 thoughts on “JavaScript Variables – var, let, const

Leave a Reply

Your email address will not be published. Required fields are marked *