New post Need visibility? Apply for a FREE post for your Startup.  Apply Here

Now you know

How to use var, let, and const in JavaScript

2 Mins read

JavaScript is a high-level, interpreted programming language that is commonly used in web development. It was first created in 1995 by Brendan Eich, a developer at Netscape Communications Corporation.

JavaScript allows developers to create dynamic and interactive web pages by providing a way to add interactivity and behavior to HTML and CSS. It is also used for server-side programming and building desktop and mobile applications.

Variables in JavaScript

In JavaScript, a variable is a container that stores a value, which can be used throughout your code. Variables allow you to assign a name to a value, making it easier to refer to and manipulate in your code.

Image source : codeburst

To declare a variable in JavaScript, you can use the var, let, or const keywords. Var is the original way to declare a variable, while let and const were introduced in the ECMAScript 6 specification.


The var keyword was traditionally used to declare variables in JavaScript before the introduction of let and const in ES6. Variables declared with var are function-scoped, meaning they are only accessible within the function in which they are declared.

Here is an example of declaring a variable with var:

function myFunction() {
  var x = 10;

Variables in JavaScript can hold any data type, including strings, numbers, booleans, objects, and more. You can also declare multiple variables at once using a comma-separated list:

var firstName = "John", lastName = "Doe", age = 30;


The let keyword was introduced in ES6 and is used to declare block-scoped variables, which are only accessible within the block of code in which they are defined. This is different from var, which is function-scoped. Here is an example of declaring a variable with let:

function myFunction() {
  let x = 10;
  if (true) {
    let x = 20;
    console.log(x); // output: 20
  console.log(x); // output: 10

In the example above, we declared a variable x using let inside the function myFunction. We then created a new block of code using an if statement, where we declared another variable x using let. Since this new variable x is only accessible within the block of code created by the if statement, it does not affect the value of the variable x declared earlier in the function.


The const keyword was also introduced in ES6 and is used to declare constants, which are read-only and cannot be reassigned. Like let, const is also block-scoped. Here is an example of declaring a constant:

function myFunction() {
  const x = 10;
  // This will throw an error: TypeError: Assignment to constant variable.
  x = 20;

In the example above, we declared a constant variable x with the value 10. We then tried to reassign the value of x to 20, which resulted in a TypeError since x is a constant and cannot be reassigned.

It is important to choose the appropriate keyword when declaring variables in JavaScript, depending on the scope and mutability you need for the variable.

632 posts

About author
When I'm not reading about tech, I'm writing about it, or thinking about the next weird food combinations to try. I do all these with my headphones plugged in, and a sticky note on my computer with the words: "The galaxy needs saving, Star Lord."
Related posts
Now you know

Website layouts you should explore in your design

3 Mins read
As a UI/UX and website designer, you will be faced with several decisions to make about which layout to choose for your…
How TosNow you know

Understanding the technology behind File Systems

3 Mins read
Have you ever wondered how your computer stores and organizes all those amazing files? It’s all thanks to something called a “file…
Now you know

React vs. Angular: which is better for projects and easier to learn

3 Mins read
JavaScript is an important tool in the field of web development, and as such, two JavaScript frameworks have taken center stage: React…
Get powered up with Techpadi Newsletter

Be the first to know what's happening in the African tech space

Leave a Reply

Artificial IntelligenceNow you knowRandom

The Controversy Surrounding ChatGPT: Universities Ban AI Tool Over Academic Integrity Concerns.