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.

883 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
Now you know

What you need to know about the O.MG Cables

2 Mins read
When you’re asked what age we’re in, you’d likely say we are in the digital age – an age where our devices…
Now you know

The man who scammed two tech giants of $122 million

1 Mins read
There have been several stories about people running elaborate schemes to defraud people and large companies. Many of these schemes have gone…
ArticleNow you knowRandom

SSD vs. HDD: What's The Difference?

3 Mins read
As we continue to bask in the euphoria of recent technological trends in this age of digitization, data generation, manipulation, information processing…
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

×
Now you know

Who is a hacker?