DEV Community

Osama
Osama

Posted on

Javascript 1O1 series : Variables vs Constants, and Let keyword.

speedy sense

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

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

if you want to read and understand what scope is.

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 :


References and useful links :

Var, Let, and Const – What's the Difference?

A lot of shiny new features came out with ES2015 (ES6). And now, since it's 2020, it's assumed that a lot of JavaScript developers have become familiar with and have started using these features. While this assumption might be partially true, it's still possible that some of these features

favicon freecodecamp.org

Difference between var, let and const keywords in JavaScript - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

favicon geeksforgeeks.org

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, github and my portfolio.

Top comments (0)