What is the difference between var and let in javascript mcq?

In the early days of JavaScript, there was only one way of declaring variables and that was using the var keyword. A variable declared with var is defined throughout the program. One of the issues with using the var keyword was redeclaring a variable inside a block will also redeclare the variable outside the block. 

With the introduction of ES6 in 2015 two more keywords, let and const came into the picture. var and let are both used for variable declaration in javascript but the difference between them is that var is function scoped and let is block scoped. Variable declared by let cannot be redeclared and must be declared before use whereas variables declared with var keyword are hoisted. 

An example will clarify the difference even better 

Example 1: Here we will see the use of var.

Javascript

    console.log[x];

    var x=5;

    console.log[x];

Output:

undefined
5 

Example 2:  Here we will see the use of let.

Javascript

    console.log[x];

    let x=5;

    console.log[x];

Output:

ReferenceError: Cannot access 'x' before initialization

 Code 1: Let’s see the code in JavaScript.

Javascript

    var x = 5;

    document.write[x, "\n"];

    let y = 10;

    document.write[y, "\n"];

    document.write[z, "\n"];

    var z = 2;

    document.write[a];

    let a = 3;

Output:

 

Code 2: In the following code, clicking start will call a function that changes the color of the two headings every 0.5sec. The color of the first heading is stored in a var and the second one is declared by using let. Both of them are then accessed outside the function block. Var will work but the variable declared using let will show an error because let is block scoped. 

Javascript

GeeksforGeeks

GeeksforGeeks

Start

    function colour[] {

        setInterval[function[] {

            if [document.getElementById['var'].style.color == 'black']

                var col1 = 'blue';

            else

                col1 = 'black';

            if [document.getElementById['let'].style.color == 'black'] {

                let col2 = 'red';

            } else {

                col2 = 'black';

            }

            document.getElementById['var'].style.color = col1;

            document.getElementById['let'].style.color = col2;

        }, 500];

    }

Output:

 

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples. 

Let us understand the differences in a tabular form:

 

var

1. The var is a keyword that is used to declare a variable The let is also a keyword that is used to declare a variable.
2.

Syntax -:

var name = value;

Syntax -:

let name = value;

3. The variables that are defined with var statement have function scope. The variables that are defined with let statement have block scope.
4. We can declare a variable again even if it has been defined previously in the same scope. We cannot declare a variable more than once if we defined that previously in the same scope.
5. Hoisting is allowed with var. Hoisting is not allowed with let.
6.

Example -:

var websitename = “geeksforgeeks”;

Example -:

 let x = 69;

7. var is an ECMAScript1 feature. let is a feature of ES6.
8. Its supported browsers are: Chrome, Internet Explorer, Microsoft Edge, Firefox, safari, opera Its supported browsers are -: Chrome49, Microsoft Edge12, firefox44 , safari11, opera36

We have an article on how to declare variables in different ways in JavaScript.


What is the difference between VAR and let in JavaScript?

The main difference between let and var is that scope of a variable defined with let is limited to the block in which it is declared while variable declared with var has the global scope. So we can say that var is rather a keyword which defines a variable globally regardless of block scope.

What is the difference between VAR and let keyword in JavaScript Mcq?

Both of these are useful for the purpose of declaration in JavaScript, but there is a significant difference between var and let in JavaScript. Here, the let keyword is block-scoped while the var keyword is function scoped.

What is the difference between const and let keyword Mcq?

Hoisting of const var declarations are globally scoped or function scoped while let and const are block scoped. var variables can be updated and re-declared within its scope; let variables can be updated but not re-declared; const variables can neither be updated nor re-declared.

What is difference between LET and VAR in TypeScript?

The let statement is used to declare a local variable in TypeScript. It is similar to the var keyword, but it has some restriction in scoping in comparison of the var keyword. The let keyword can enhance our code readability and decreases the chance of programming error.

Chủ Đề