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 7969

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

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T08:45:08+00:00 2024-11-28T08:45:08+00:00

Day 1: A beginners take on JS scope. Var vs Let vs Const.

  • 60k

I am a beginner.

So I have recently begun on my challenge to find a web dev role by the end of the year.
Along side this goal comes all the new and exciting things you have to learn, and one of the things that is incredibly important is understanding ES6 in all it's glory.

So, what is the difference between Var, Let and Const?

There are a fair few differences and use cases for each, and all are important but to properly answer this question you need to understand the concept of “scope”.

A brief aside about scope.

Without getting into too much detail, scope is where variables in your code can be “accessed” or “referenced”.

The three types of scope that are important here though, are “Global”, “Functional”, and “Block” scope.

Block scope is anything that is surrounded by { } curly braces, more specifically an if/while/for statement might be where this arises:

{   let x = 2; } if(1 > 2){   const y = 5; } 
Enter fullscreen mode Exit fullscreen mode

Both of the above are examples of block scope.

Functional scope or Local Scope (in JavaScript they are essentially the same) is when you declare your variables inside a function that cannot be accessed outside said function:

function add(x, y){   var z = x + y;   return z; } function subtract(x,y){   let z = x - y;   return y; } function multiply(x,y){   const z = x * y;   return y; } //z cannot be accessed here. 
Enter fullscreen mode Exit fullscreen mode

All of the three types of variable here seem to act the same as they cannot be accessed outside of the functions due to Functional/Local scope.

Last but not least is global scope which , as you can imagine, can be referenced anywhere your code as it is declared outside a function or block.

let x = 0; // x can be accessed here function myFunction() {   x++; // x can also be accessed here } 
Enter fullscreen mode Exit fullscreen mode

Now that we understand what scope in JavaScript looks like, perhaps we should get back on track.

Hurry up and tell me how Var, Let and Const are different!

Ok ok!

Well with the introduction of Let and Const in ES6, block scoping became a concern and also a benefit to using these keywords.
Quick Note:
To declare a variable is to create it with a keyword:

var x;  let y; 
Enter fullscreen mode Exit fullscreen mode

To define a variable is to give it a value:

var x = 1; let y = 2; const x = 3; 
Enter fullscreen mode Exit fullscreen mode

So in one sentence each:

var (the og variable) can be declared and defined as many times as you wish.

let can be declared a single time but defined as many times as you wish, it also has block scoping.

const can be declared and defined ONLY once, but it also has block scoping and MUST be defined and declared at the same time.

Easy eh?

And now for the details

Var is now not the variable you should declare as a default instinct because of it's global scope. Scoping is important for good practice code, so as your code gets bigger and longer and more complex, you might run into some redeclaration problems:

var x = 0; function myFunction(){   var x = 2;   //Do this and that }  
Enter fullscreen mode Exit fullscreen mode

This is ALLOWED but it should be avoided as all costs, because it is confusing and generally leads to bad practice. Nowadays it is recommended to use the let keyword in var's place.

Let as mentioned previously has block scope, but can be redefined as many times as you wish:

let x = 0; x = 5; x = 10; //This is ok but...  let y = 0; let y = 5; //This is NOT ok 
Enter fullscreen mode Exit fullscreen mode

and now for block scope:

if(check_something){   let y = 5;   console.log(y);   //Expected: 5; } console.log(y); //Expected: undefined. 
Enter fullscreen mode Exit fullscreen mode

Finally, const is the most interesting keyword because it has block scoping, must be declared and defined at the same time, and cannot be redeclared or redefined at any point, but it still has so many use cases.
const should be used when _defining arrays and objects because each value inside the array is not in fact a constant but the reference to the const ITSELF is constant.
Some examples:

const names = ["Nick", "Emma", "Dan"]; names = ["Tim", "Tom", "Tam"]  //ERROR: this is a redefine so will not work. const ages = [20, 30, 40]; const ages = [50, 60, 50]; //ERROR: this is a Redeclaration so will not work. 
Enter fullscreen mode Exit fullscreen mode

However, using an object and an array as an example these both are ok:

const names = ["Nick", "Emma", "Dan"]; names[0] = "Not Nick"; const user = {name:"Nick, age:"29", hobby: "board games"}; user.age = 30; //All valid code. 
Enter fullscreen mode Exit fullscreen mode

A final note is that const, like let, is block scoped but I won't bore you with more examples.

That's all folks.

This has been me, as a beginner attempting to revise and explain briefly what scoping, and the three keywords var, let and const mean as simply as possible.
I am sure I've missed so many interesting points, but hey maybe next time!
For more details, read MDN or W3Schools as always ;).

beginnersjavascriptnodewebdev
  • 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 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 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.