# Variable in Javascript

The concept of variable is very essential in every programming language, at the end of this article you should be able to understand

1. What is a Variable?
    
2. 3 methods to declare variables in Javascript.
    
3. Scope
    
4. Function, Global, and Block Scope.
    

## what is a Variable?

A variable is a storage location that is used to hold a value, imagine you have a shelf in your room where you keep different boxes and inside those boxes 📦 you store different items in them, the box is like the variable and the items are the values the box hold.

You can think of the process of storing a letter in the box as assigning value to a variable, you can also equate the process of accessing the value of a variable as opening the box and looking at what is inside.

```javascript
var box = letter1;
// WHERE box IS THE VARIABLE
// "letter1" IS THE VALUE STORED IN THE VARIABLE
// VAR IS THE KEYWORD FOR DECLARING box AS A VARIABLE.
```

And then you could access the value of the `box` variable like this

```javascript
console.log(box); //output : letter1
```

Just like you can put different things in the box that you keep on your shelf, you can also assign different values to a variable in JavaScript. You could, for example, change the value of the `box` variable to the `letter 3`.

## Methods of declaring a variable in Javascript

Variables in javascript can be declared using VAR, LET, AND CONST keywords, we would take a look at them individually.

### *Var Keyword*

`var`: This is the traditional way of declaring variables in JavaScript. It is used to declare variables that are either `Function Scope` (that is they are only accessible within the function they exist) or `Global Scope` (that is they are accessible anywhere in the code).

```javascript
var x = 10; //Global scope

function () {
    var y = 11;
} // function scoped
```

### *Let Keyword*

`let`: This is a newer way of declaring variables in JavaScript, introduced in ECMAScript 6 (ES6). It is used to declare variables that are `Block Scope`, meaning that the variables are only available within the block in which they are defined.

```javascript
let y = 20;
```

### *Const Keyword*

`const`: This is also a newer way of declaring variables in JavaScript, introduced in ECMAScript 6 (ES6). It is used to declare read-only variables, meaning that the variable's value cannot be reassigned.

```javascript
const z = 30;
```

## Conclusion

It's important to note that `var` is function-scoped, while `let` and `const` are block-scoped. This means that variables declared with `var` can be accessed within the entire function in which they are declared, while variables declared with `let` and `const` are only accessible within the block in which they are defined.

*Happy Reading!!!*

I'd love to connect with you via [**Twitter**](https://twitter.com/Timmy_deey) | [**LinkedIn**](https://www.linkedin.com/in/timmy-dahunsi-598010176/) | [**GitHub**](https://github.com/Timmydee) |
