JavaScript Variables
Published: 2022-11-03 15:38:04

## **What is a variable?**
Data that may vary over time can be stored in memory by using variables. It may not seem like much, but it is what causes a program to respond to keystrokes made by a human (or any other device really).
A variable has a name so that we may use it in the code whenever we need it. It has worth as well. Say, for instance, that the variable number has the value: 3. The computer will insert the data stored in memory where you wrote a number in your code. It is 3 in our example.
A variable's data might vary with time, as was previously mentioned. Therefore, if you run the software again or even during the same run, number can have the value 5. This is the origin of the word "variable." It is a piece of code that may change based on a number of variables.

## **How to create a variable?**
In JavaScript, the definition of a variable (which is when you create a variable) is made this way:

// keyword name = value
var a = 2
let b = 4
const c = 6

The code above creates/defines 3 variables a, b and c which have for values 2, 4 and 6 respectively.

This is the quick and simplified version of two other actions we've made on the variable. The definition of the variable equals 1) the declaration of the variable, saying we want to create one and keep some memory space for it specifically and 2) the initialization of the variable: giving the variable we just declared a value for the first time.
**
let a // declaration of the variable a

a = 1 // initialization of the variable a
**
When these two actions are separated, the variable exists as soon as it is declared but doesn't have a value unless the initialization has been done. We then say that the variable is undefined because the process of definition is not completed. The variable equals undefined.

## **Differences between var, let, const**

 **VAR**


The variable declaration var has been used with javascript since its inception. The var variable (also the let and constvariables) are used to store different types of data. A variable can store data such as strings, numbers, booleans, objects, functions, and more.

var myName = 'bob';
var myAge = 34;

The example above show the different types of data that can be stored in variables. All the variables above are declared with the var keyword. Declaring a variable with var and outside of any other blocks of code places it within the global scope, meaning the variables can be accessed from any other part of the program and is available globally within the program.

 **LET**

The variable declaration let is similar to var in many ways, but include some key difference when it comes to access and scope. A variable declared within the global scope using let can be accessed anywhere in the program. This behavior is just like what happens with the var variable declaration.
Also, just like with var, variables declared with let within a function scope are only accessible within the function scope and will cause an error when attempting to access outside of the function scope.
However, when using block scopes, any variable declared with let inside a block is not accessible outside of the block and will cause an error. This is a key difference to variables declared with the var keywords, which can be accessed outside of a block.

 **CONST**

The variable declaration const is meant to be used for data that will not change - in other words, data that will remain constant. Just like with var and let, variables declared outside of a function or block with const are available to the global scope.
And just like variables declared with let, a variable declared with const is not accessible outside of either function scope or block scope and will cause an error.
An important note about using variables declared with const is that they cannot be reassigned. This applies to const variables that have data including values, arrays, or objects.

## **JavaScript Scope**
JavaScript has a feature called scope. The concept of the scope in not that easy to understand for many new developers but I will try my best to explain them in simple way. Understanding scope will make your code better and key to writing bulletproof code and being a better developer.

**What Is Scope?**
In JavaScript scope is the set of variables, functions and objects you have accessed to. Scope refers to the current context of your code. scope can be defined globally and locally.

**GLOBAL SCOPE**
Before writing code in JavaScript, you are in what we call Global Scope. If we declare a variable, It’s considered as Global Variable.

**LOCAL SCOPE**
There is typically one global scope and each function defined has its own local scope. Any function defined with in another function has a local scope of the parent function.
If I create function and defined some variables, those variable will be the local variables of that function.