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.

`var

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;

`let

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.

`const

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.

Don’t miss any tech news ever!

We don’t spam! Read our privacy policy for more info.

792 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."
Articles
Related posts
ArticleNow you knowRandom

5 Reasons Why Telegram Is Better Than WhatsApp

2 Mins read
Within the last decade, I don’t know if there be any micro-messaging app as popular as WhatsApp and Telegram. They have become…
Now you know

Bitcoin halving: What it is and how it affects the cryptocurrency

3 Mins read
Bitcoin is unique among cryptocurrencies for many reasons, not least of which is its programmed monetary policy. At the heart of this…
ArticleMobileNow you knowRandom

5 Essential Android Apps iPhones Should Have

3 Mins read
The battle for supremacy between the iPhone and Android mobile phones seems ending. The argument for and against whichever brand you support…
Newsletter Subscription

🤞 Don’t miss any update!

We don’t spam! Read more in our privacy policy

Join our Telegram channel here - t.me/TechpadiAfrica

Leave a Reply

×
Artificial IntelligenceNow you knowRandom

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