Techpadi

How to use var, let, and const in JavaScript

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.

Exit mobile version