Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JavaScript»What is difference between var, let and const in JavaScript?

    What is difference between var, let and const in JavaScript?

    RahulBy RahulJune 30, 20223 Mins Read

    A variable declaration is simply telling the computer that a variable exists and what value it should start with. Similar to other programming languages JavaScript also allows for the declaration of variables.

    There are three keywords in JavaScript that can be used to declare variables: let, var, and const. Each keyword has different rules and implications for how the variables they create can be used.

    1. let: The let keyword declares a block-scoped local variable, optionally initializing it to a value.

      Block-scoped means that the variable is only available within the block it was declared in, which is usually denoted by curly braces {}.

    2. var: The var keyword declares a function-scoped or global variable, optionally initializing it to a value.

      Function-scoped means that the variable is only available within the function it was declared in. Global variables are available throughout your entire code.

    3. const: The const keyword declares a block-scoped, immutable constant variable, i.e. a variable that can’t be reassigned.

      Constants are also called “immutable variables”, but that’s a bit of a misnomer since they are actually variables – just ones that can’t be reassigned.

    What is difference between var, let and const?

    The var keyword is the oldest way of declaring variables in JavaScript and is supported by all browsers. The let and const keywords are newer additions to the language and are not supported by older browsers.

    If you need to support older browsers, you can use var instead of let or const. If you don’t need to support older browsers, you can use let or const. If you want your variable to be immutable, use const.

    Here are some examples:

    1
    2
    3
    4
    5
    6
    7
    var x = 1;
    let y = 2;
    const z = 3;
     
    x = 4; //OK
    y = 5; //OK
    z = 6; //Error

    As you can see, var and let variables can be reassigned, but const variables can not.

    Another difference between var and let/const is that var variables are function-scoped, while let and const variables are block-scoped.

    This means that var variables are only available within the function they were declared in. For example:

    1
    2
    3
    4
    5
    6
    function foo() {
      var x = 1;
    }
     
    foo();
    console.log(x); // ReferenceError: x is not defined

    On the other hand, let and const variables are only available within the block they were declared in. For example:

    1
    2
    3
    4
    5
    6
    7
    8
    function foo() {
      let y = 2;
      const z = 3;
    }
     
    foo();
    console.log(y); // ReferenceError: y is not defined
    console.log(z); // ReferenceError: z is not defined

    So, to sum up, the main differences between var, let and const are:

    • var is function-scoped while let and const are block-scoped.
    • var variables can be reassigned while let and const variables can not.
    • var variables are declared using the var keyword while let and const variables are declared using the let and const keywords respectively.
    • const variables are immutable while let and var variables are not.
    js variable
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleWhat is CPU? – Definition, Types and Parts
    Next Article How to Accept User Input in Python

    Related Posts

    How to declare boolean variable in shell script

    Updated:July 25, 20221 Min Read

    How to Replace String in JavaScript

    Updated:June 13, 20222 Mins Read

    How To Set/Create Environment and Shell variables in Linux

    Updated:July 10, 20215 Mins Read

    JavaScript Program to Add Two Numbers

    Updated:June 9, 20222 Mins Read

    Remove First Character from String in JavaScript

    1 Min Read

    JavaScript Program to Remove Duplicate Array Elements

    Updated:July 20, 20211 Min Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Import GPG Keys on Ubuntu & Debian (without apt-key)
    • How To Install Google Chrome On macOS
    • How to Install Minecraft on Ubuntu 22.04 & 20.04
    • Running a Cron job every Sunday (Weekly)
    • Running Multiple Commands At Once in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.