Difference between Var and Let

Home /

Table of Contents

Variable

In JavaScript, there are three ways to declare variables: using ‘var‘, ‘let‘, and ‘const‘.

  1. var‘ keyword: This is the original way to declare variables in JavaScript. Variables declared with ‘var‘ are function-scoped or globally scoped.

Syntax: 'var variable_Name = value;'

Example:

JavaScript
var name = "John";
var age = 30;
  1. let‘ keyword: Introduced in ECMAScript 6, variables declared with ‘let‘ are block-scoped. This means they are only accessible within the block they are declared in, such as a for loop or an if statement.

Syntax: ‘let variableName = value;

Example:

JavaScript
let name = "John";
let age = 30;
  1. const‘ keyword: Also introduced in ECMAScript 6, variables declared with ‘const‘ are block-scoped and their value cannot be reassigned.

Syntax: ‘const variableName = value;

Example:

JavaScript
const name = "John";
const age = 30;

It’s important to choose the appropriate variable declaration depending on the use case. Use ‘var‘ if you need function or global scope, use ‘let‘ if you need block scope and plan to reassign the value, and use ‘const‘ if you need block scope and don’t plan on reassigning the value.

Difference between ‘Let’ and ‘Var’

In JavaScript, both ‘let‘ and ‘var‘ are used for declaring variables, but they have some differences:

  1. Scoping: Variables declared with ‘var‘ have function scope or global scope, whereas variables declared with ‘let‘ have block scope.
  2. Hoisting: Variables declared with ‘var' are hoisted to the top of their scope, which means that they can be accessed before they are declared. Variables declared with ‘let' are not hoisted, which means that they cannot be accessed before they are declared.
  3. Re-assignment: Variables declared with ‘let' can be reassigned a new value, while variables declared with ‘const' cannot be reassigned. Variables declared with 'var' can also be reassigned.

Here’s an example that demonstrates the difference between ‘let' and ‘var':

JavaScript
function example() {
  var x = 1;
  if (true) {
    var x = 2; // This reassigns the variable declared with `var` in the function scope
  }
  console.log(x); // Output: 2
}

function example2() {
  let y = 1;
  if (true) {
    let y = 2; // This creates a new variable with block scope
  }
  console.log(y); // Output: 1
}

In the first example, the variable ‘x‘ is declared with ‘var‘ in the function scope, and then it is reassigned inside the ‘if‘ statement. When we log ‘x‘ to the console, we get the re-assigned value of ‘2‘.

In the second example, the variable ‘y‘ is declared with ‘let‘ in the function scope, and then a new variable ‘y‘ is declared with ‘let‘ inside the ‘if‘ statement, which has block scope. When we log ‘y‘ to the console, we get the value of the variable declared in the function scope, which is ‘1‘.

Declare Array

In JavaScript, you can declare an array using the following syntax:

JavaScript
var arrayName = [element1, element2, ..., elementN];

Here’s an example:

JavaScript
var fruits = ["apple", "banana", "orange"];

This creates an array called ‘fruits‘ with three elements: “apple”, “banana”, and “orange”. You can access individual elements of the array using their index:

JavaScript
console.log(fruits[0]); 
console.log(fruits[1]); 
console.log(fruits[2]); 

Output:

apple
banana
orange

You can also add elements to an array using the ‘push()‘ method:

JavaScript
fruits.push("pear");
console.log(fruits); 

Output:

apple
banana
orange
pear

You can remove elements from an array using the ‘pop()‘ method:

JavaScript
fruits.pop();
console.log(fruits);

Output:

apple
banana
orange

You can get the number of elements in an array using the ‘length‘ property:

JavaScript
console.log(fruits.length);

Output:

3

There are many other methods and properties you can use with arrays in JavaScript, but these are some of the basics.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article