Var, let and const
Published: 2022-02-23 13:16:45

Hi peeps 👋! I'm here again with a shot which will explain to you, the difference between `var`, `let` and `const` in creating a **JavaScript** _variable_.

>I am Akande Olalekan Toheeb, a Nigerian self-taught developer who is passionate about solving problems and helping others with the easiest way in problem solving.

Let's get started 😍!

## Overview

>Do you know what a **Variable** is ?

In JavaScript, A **Variable** is a way of storing and keeping track of values so they can be later used in the program. This definition is almost the same in all programming language.

Nearly all program runs on data and that is why the usage of variable is very important in writing codes. 

A variable can store all kinds of data types which includes: _Strings, Numbers, Arrays, Objects_ e.t.c

## How to declare a **Variable** 

There are three methods:

- The first way is to use the `var` keyword which is the most common method
- Using the `let` keyword is the second
- Using the `const` keyword emerges the last method in creating a variable.

>Syntax `let variableName = '';`

### Some tips in Declaring Variable Names

Giving your _Variable_ a name that corresponds to its value will be of great help when writing a code. This is because you won't have to stress yourself when next you need to use the stored in the variable. Below are some other tips in setting the name:

-  _A variable is case sensitive_ so you have to very be careful while naming, _name is not Name_. 
-  Always use the _camelCase_ when declaring variables. 
 
 >In _camelCase_, multi-word variable names have the first word in lowercase and the first letter of each subsequent word is capitalize. e.g `var aMultiWordVariableName;`
 
-  Do not begin a _variable name_ with an underscore.
-  Do not use variable names that consist of a single character. Short variable names are only allowed for loop counters.

Examples:

```

var name= "Akande Olalekan Toheeb";
let authorAge = 22;
const gender = "Male";

```

## Difference between `var`, `let`, `const`

>The three methods of declaring variables have their differences and this is explained in this section.

One of the biggest problems with declaring variables with the `var` keyword is that you can easily overwrite variable declarations. 

For example:

```

var name = "AKANDE";
name = "Olalekan Toheeb";
console.log(`My name is ${name}`);

```

In the above lines of code, the code will print **My name is Olalekan Toheeb** to the console. But why 😮???

> This simply because the value of the variable has been overidden and this happened in the second line of code.

A keyword called `let` was introduced in **ES6**, a major update to JavaScript to solve this issue.

Keyword `let` is very similar to `var` but it doesn't allow _overidden_ of the value. The value can only be added to with the **addition operator `+`**. e.g

```
var name = "Akande";
name = name + "Olalekan Toheeb"
console.log(`My name is ${name}`);

```

This will print _My name is Akande Olalekan Toheeb_ to the console.


`const` has all awesome features that `let` has, with an added bonus that variables declared with `const` are _read-only_. They are a constant value, which means that once a variable is assigned with `const`, **it cannot be override nor reassigned**.

```
const name = "AKANDE";
console.log(`My name is ${name}`);
name = "Olalekan Toheeb";
console.log(`My name is ${name}`);

```

The above code will print _My name is AKANDE_ to the console and then an error. This is because of the `const` used in the declaring the variable.

Thank you for reading, have a nice day!

Your appreciation is my motivation 😊 - Give it a like

- Follow me on [Twitter](https://twitter.com/devtoheeb).
- Click [here](https://www.linkedin.com/in/akande-olalekan-toheeb-2a69a0221/) to connect on Linkedln.