Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 858

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T02:45:08+00:00 2024-11-25T02:45:08+00:00

Different types of variable declarations in JavaScript – var, let and const.

  • 62k

Hi everyone. Here is my first blog in JavaScript. This blog is about the ways that can be used to declare variables in JavaScript. I know, as a beginner, it is difficult to understand the declaration of variable scope.

So.. I am here to make it easy. 
And here you go…………….

In JavaScript, there are four ways to declare a variable.

Yes, you heard it right… let's elaborate.

1. Assign variable without declaring it in JS:

Yes, we can assign the variable without using var, let and const keywords. And when we do that, it becomes a global property of window object. 

But remember, this is not recommended.

x = 78 console.log(a); // output- 78 
Enter fullscreen mode Exit fullscreen mode

But this is not applicable when we use the strict mode.

'use strict' x = 78; console.log(a); // output- Uncaught ReferenceError: x is not defined 
Enter fullscreen mode Exit fullscreen mode

2. var:

When JavaScript was first introduced, developers only used var to declare variables.Let's see a few examples.

  • Hoisting: We can use variables before we declare them in our code.  We would get undefined as an output, but JavaScript would not throw an error if we are using var keyword.
'use strict'  function varExample() {     console.log(x); // output- undefined     for ( var i = 0; i< 3; i++){         var x = i;     }; }; varExample(); 
Enter fullscreen mode Exit fullscreen mode

  • Global Scope: var can be used globally.  We have declared x without any assignment, so for the first output we got undefined. And in the last output, we got 2 after assigning the value.  
'use strict'  var x function varExample() {     console.log("First check", x); // output- First check undefined.     for ( var i = 0; i< 3; i++){         x = i;     };     console.log("last check", x); // output- 2 }; varExample(); 
Enter fullscreen mode Exit fullscreen mode

  • Functional scope: var has a functional scope, which means we can access the var variable anywhere within the function.  For example, x is declared within the for loop block but x is accessible outside of the block.
'use strict'  function varExample() {          for ( var i = 0; i< 3; i++){         var x = i;     };     console.log(x); // output- 2 }; varExample(); 
Enter fullscreen mode Exit fullscreen mode

But x can't be accessible outside of the function.

 

'use strict'  function varExample() {     for ( var i = 0; i< 3; i++){         var x = i;     };      }; console.log(x); // output- Uncaught ReferenceError: x is not defined varExample(); 
Enter fullscreen mode Exit fullscreen mode

  • Re-Declaration is allowed: We can use var to re-declare or update the variable.
'use strict'  function varExample() {     var x = 10;     var x = 19;     console.log(x); // output- 19     x = 60;     console.log(x); //output - 60 }; varExample(); 
Enter fullscreen mode Exit fullscreen mode

3. let:

let is introduced in ES6.Let's see the same examples with let as well.

  • Hoisting: We can't use variables before we declare them in our code using the let keyword. While trying to access 'c', we are getting a reference error. This is called Temporal Dead Zone(TDZ). until the value gets initialized to the variable TDZ continues.
'use strict'  function varExample() {     console.log(c); // output-Uncaught ReferenceError: c is not defined. TDZ continues here.     for ( var i = 0; i< 3; i++){ // TDZ continues here.         let c = i; // TDZ ended here.     }; }; varExample(); 
Enter fullscreen mode Exit fullscreen mode

  • Global Scope: let is having global scope too. And it would show the same output as var.
'use strict'  let c; function varExample() {     console.log(c); // output - undefined     for ( var i = 0; i< 3; i++){         c = i;     };     console.log(c); // output - 2 }; varExample(); 
Enter fullscreen mode Exit fullscreen mode

  • Block Scope: The scope of a let variable is only block scope in JavaScript. Let's check out the below example. x is declared inside the for loop block, so when we are trying to access the x outside of the block, we are getting an error.
'use strict'  function varExample() {          for ( let i = 0; i< 3; i++){         let x = i;     };     console.log(x); // output - uncaught ReferenceError: x is not defined }; varExample(); 
Enter fullscreen mode Exit fullscreen mode

  • Re-declaration: For let variable, re-declaration is not permitted.
'use strict'  function varExample() {     let t = 10;     let t = 19; // output - Uncaught SyntaxError: Identifier 't' has already been declared.     console.log(t);     t = 60;     console.log(t); }; varExample(); 
Enter fullscreen mode Exit fullscreen mode

But updating variables are allowed.

'use strict'  function varExample() {     let t = 10;     console.log(t); // output - 10     t = 60;     console.log(t); // output - 60 }; varExample(); 
Enter fullscreen mode Exit fullscreen mode

3. const:

The variable const is the same as the variable let. const is also introduced in ES6. The only difference is that “const” can't be updated once it is declared.

'use strict'  const x = 8 function varExample() {     x = 11; // output - Uncaught TypeError: Assignment to constant variable.     console.log(x);      }; varExample(); 
Enter fullscreen mode Exit fullscreen mode

But if we are declaring a variable like below, then x has a different scope—one is global and another is block scope,so JavaScript treats them as different variables.

'use strict'  const x = 8 function varExample() {     const x = 11;      console.log(x); // output - 11 (block scope)      }; console.log(x); // output - 8 (global scope) varExample(); 
Enter fullscreen mode Exit fullscreen mode

Summary:

  • We should not use variables without the var, let and const keywords.

  • It is a good practice to use const unless you don't have anything to override.

  • Try to avoid var until you don't have a browser compatible issue. 

  • let and const does not create properties of window object when declared globally (in the top scope).

  • Our first priority should be to use const variable to avoid value re-assignment errors in our code Unless we are pretty sure that we will not be changing its value later. Then let should be our second priority. 

Image description

beginnersjavascriptreactwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.