Javascript 1O1 series : Variables vs Constants, and Let keyword.
Tags: javascript, beginners, programming, tutorial
Published: 2022-04-18 10:38:17

![speedy sense](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/68erogewukyr72bfdqcn.png)
 
**You’ll learn :**
about constant and const keyword and variables declared with let keyword and the differences between these types of variables.

---

**constant :**
we all know about variable and it is container that hold specific value during program’s execution, and this value can be changed whenever to whatever the programmer want.
constants are the same thing but the only difference is the const value can not be changed during the code execution, the initiated value when we declare the constant will remain the same the whole time.

> Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment (i.e. by using the assignment operator), and it can't be redeclared (i.e. through a variable declaration). However, if a constant is an object or array its properties or items can be updated or removed. MDN.

> The const keyword was introduced in ES6 (2015), Variables defined with const cannot be Redeclared, Variables defined with const cannot be Reassigned. w3Schools.

**Syntax :**

`const identifier = anyValue;
const x = 10 ;`

const keyword , identifier, assignment operator, any value
variable declared by const keyword, its value can not be changed (object’s properties and array’s elements still can be updated or removed), it can not be redeclared, and last thing its scope will be Blocked-Scope.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5544t1rrc1w30ktyy4r.png)
 

you’ll learn about scopes in later tutorial

---


**Let keyword :**

The let statement declares a block-scoped local variable, optionally initializing it to a value. MDN
variables declared with let keyword and variables declared with var do the same thing , they are variables and its rules pretty much the same, the key difference is their scope limitation, var is function-scope and let is blocked scope.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oq0srh9arwacvo0uo4h3.png)
 
if you want to read and understand what scope is.
{% embed https://www.w3schools.com/js/js_scope.asp %}

**Rules for let variables :**

> - The let keyword was introduced in ES6 (2015).
> - Variables defined with let cannot be Redeclared.
> - Variables defined with let must be Declared before use.
> - Variables defined with let have Block Scope. w3schools

Syntax :

`let identifier = anyValue;
let x ;`

let keyword, identifier, assignment operator and a given value.
you can drop the initiated value when declaring let variables and can assign it later, the default value will be undefined when no value assigned to the let variable. expression.

## Let vs Var vs Const :

let and var variables can be reassigned, const can’t
var variables can be redeclared, let and const can’t
let and const are blocked-scope, while var is a function-scope
let and var can be declared without initial value, const can’t
let and var default values without initialization values will be undefined

**More :**

{% embed https://medium.com/swlh/the-difference-of-var-vs-let-vs-const-in-javascript-abe37e214d66 %}

---

## References and useful links :

{% embed https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/ %}

{% embed https://www.geeksforgeeks.org/difference-between-var-let-and-const-keywords-in-javascript/ %}

{% embed https://medium.com/swlh/the-difference-of-var-vs-let-vs-const-in-javascript-abe37e214d66 %}

---
Thanks for reading, feel free to ask any question about javascript or about this series, I appreciate any feedback or advises to improve My content.
find me on [twitter](https://twitter.com/Osam1010), [github](https://github.com/osama865/) and [my portfolio](http://o-portfolio.netlify.com/).