Use of the Javascript keywords var, let, and const in a variable declaration
Tags: javascript, variables, beginners, programming
Published: 2023-03-20 05:31:36

#Pre-requisites
This article is targeted at beginners who want to learn about keywords, var, let and const in variable declaration.
Before diving into this article, make sure that you understand the 
**camelCase** naming convention used in JavaScript. 


Data in computer science refers to everything that the computer can use to make sense of. `Undefined`, `null`, `Boolean`, `text`, `symbol`, `int`, `number`, and `object` are the eight distinct data types that JavaScript supports.
For example, computers distinguish between numbers, such as `12` and `strings`, "123”, which are collections of characters.
Also, computers can perform mathematical operations on numbers but not on strings.


In this article, we are going to learn about:
#Table of contents
1.[What is a variable?] (#variables)
2.[variable declaration using var] (#declaration)
3.[Cons of using var keyword] (#var cons)
4.[using keyword let] (#keyword let)
5.[Cons of using let keyword] (#let keyword cons)
6.[using keyword const] (#keyword const)
7.[Best practices of declaring variables] (#best practices)
8.[conclusion](#conclusion)

## What is a variable?
A variable is a storage location that allows computers to store and dynamically manipulate data. Variables, do this by using a __label__ that points out to them rather than using data itself. Any of the eight data types highlighted above may be stored in a variable.
Variables are similar to the x and y variables we use in mathematics. Therefore, variables are the simple name we use to refer to data. However, computer variables differ from mathematical variables in that they can store different values at different times. 

### Variable declaration using keyword `var`
We tell JavaScript to create or `declare` a variable by putting
keyword `var` in Infront of our desired variable name.
__for example, let us say we want a variable called myName__:
``` Put the `var` keyword in Infront of your variable name like this,
     var myName;
var myName creates a variable called `myName`. Variable names can be made up of numbers, letters, and $ or _, but may not contain spaces or begin with a number.
``` for example 
var my Name -> !!!NOT ALLOWED
var 27myName -> !!!NOT ALLOWED ```

## Cons of using the `var` keyword
 One of the biggest problems of using the keyword `var` is that you can easily overwrite variable declarations.
`var name = "Dennis";
var name = "Mbugua";
console.log(name);`
In this above example, `name` is originally declared as `Dennis’. The variable name `Mbugua` overwrites `Dennis` and the console logs out `Mbugua`.
In a small application, you might not run into this type of problem. But as your codebase becomes larger, you might accidentally overwrite a variable that you did not intend to. Because this behaviour does not throw an error, searching for and fixing bugs becomes more difficult.

## Using the `let` keyword
In Es6, a major update in JavaScript, the `let` keyword was introduced, which helps to exponentially solve this `var` problem.
` let name = "Dennis";
  let name = "Mbugua”; `
The error will be thrown onto the browser console. ‘let` keyword allows variables with the same name to be declared only once.

## Advantages of using the `let` keyword in a variable declaration
The `let` keyword does not have major problems when compared with the `var` keyword. As mentioned previously, let does not allow different variables to have the same name.

## Using the `const` keyword in the variable declaration
The keyword `let` is not the only new way to declare variables. In `Es6`, you can declare variables using the `const` keyword.
The `const` keyword has all the properties that the `let` keyword has, with a bonus that, variables declared using `const` are `read only’. They are a constant `value`. This means that variables declared with the `const` keyword cannot be reassigned once declared.
``` const met = "Cat";
    myPet = "Dog";
```
The above code will throw an error in the browser console because a `read only` variable is reassigned to a new value. You should always name variables that you don’t want to reassign using the `const` keyword. This helps when you accidentally attempt to reassign a variable that you want to remain unchanged.

### Best practices for declaring variables
**NOTE: ** It is common for most developers to name immutable variables (which means can never be resigned once declared).
for example, `const MYNAME`.MYNAME variable is immutable and is in uppercase letters. This allows another person reading your code to identify the variable as read-only.

####Conclusion
In JavaScript, data is anything that can be manipulated dynamically. Computers allow data storage using variables.
Variables are declared by putting either keywords `Var’, ‘let` or `const` in Infront of your variable name. When declaring a variable, make sure you use the right keyword appropriately.

*Happy coding* Written by **Dennis Mbugua**. **Developer and Writer**
__Follow me on Twitter__ [link](https://twitter.com/mbugua_dev)
__Follow me on GitHub__ [link](https://github.com/MbuguaDennis)