Javascript (Var vs Let vs Const)

VAR

Var is functioned-scoped and variables defined in the function CAN NOT be accessed from its outer function. But variables can be accessed from its inner function.

Children-scoped variables can USE its parents variables.

Parents can not use its children variables – because … well, yeah, children like to keep secrets 😀

Var is hoisted

Hoisting is when a variable declaration set upfront at the beginning of function execution while its assignment or value initialization will be done later, when the execution reaches the statement.

Example for Hoisting :

console.log(myDog) // undefined -- but no error raised

var myDog = 2; // although the assignment happens after the variable gets called

Basically that is the same like :

var myDog;

console.log(myDog); // undefined

myDog = 2; // because the assignment happens here

Var is Function-Scoped

function myFunction() {

  var myVar = "Noodles";

  console.log(myVar) // works! Same scope
}

console.log(myVar) // ReferenceError, the child is too shy!

VAR-declared variables can not be accessed outside the function

More subtle example :

function myFunction() {

  var myVar = "Noodles";

  if (true) {

    var myVar = "Woof!";

    console.log(myVar); // "Woof!"

    // actually, with myVar being function-scoped, we just erased the previous myVar 
    value "Noodles" for "Woof!"
}

console.log(myVar); // "Woof!" - notice the assignment in the if block has reassigned this value for good
}

console.log(myVar); // Throws a ReferenceError, myVar is not accessible outside the function.

Var can be redeclared

var myVar = 2;

var myVar = 3;

console.log(myVar) // 3

LET

Let declared variable are block-scoped, can not be redeclared in the same scope.

function myFunction() {

  let myVar = "Noodles";

  if (true) {

    let myVar = "Woof!";

    console.log(myVar); // "Woof!"

    // with let myVar being block-scoped, we just created a new variable called myVar
}

console.log(myVar); // "Noodles", it then accesses the same-scoped variable where console log is invoked
}

console.log(myVar); // Throws a ReferenceError, myVar is not accessible outside the function.

Let is not hoisted

console.log(myDog); // a ReferenceError!

let myDog = "Noodles"; // because the assignment happens after the variable gets called

Let can not be redeclared

let myVar = 2;

let myVar = 3;

console.log(myVar) // SyntaxError: Duplicate declaration

CONST

Const variables can not be reassigned and redeclared.

const dog = "Noodles"; // declaration with value assignment

dog = "Meow"; // error - reassignment is not allowed

const dog = "Meow"; // error - redeclaration is not allowed

BUT! const Object and Array are mutable!

For objects:

const dog = {

name: 'Noodles'

};

dog.name = 'Meow'; // this will work ! dog variable is not completely reassigned, but mutated

console.log(dog.name) // "Woof"

dog = "Noodles" // raises an error, because reassignment is not allowed with const declared variables

For arrays :

const dogs = [];

dogs.push('Pillow'); // this will work ! dogs array is not reassigned, but mutated

console.log(dog[0]) // "Pillow"

dogs = ["Noodles"] // raises an error, because reassignment is not allowed with const declared variables




75 Comments

  1. Modesto October 13, 2020 at 2:05 pm

    Добавлю, мне не обидно, я взрослый человек.
    Хотелось бы получить инструкцию, что делать.

  2. Mitchel November 24, 2020 at 9:48 am

    Hi, check out the collection of useful information we have prepared for you. I am sure you will find here something that will interest you! You're welcome 🙂 Mitchel

  3. sexsearch com login December 17, 2020 at 12:53 pm

    Quality posts is the important to be a focus for the viewers
    to go to see the web page, that’s what this web site is providing.

  4. affilorama warrior forum January 23, 2021 at 8:49 am

    Wonderful work! This is the type of information that are meant
    to be shared around the web. Shame on Google for nott positioning
    this post upper! Come on over and cnsult with my sitee .
    Thanks =)

    my blog post affilorama warrior forum

  5. หวย January 23, 2021 at 10:19 am

    Quality articles or reviews is the important to invite the viewers to
    pay a quick visit the website, that’s what this web site is providing.

  6. Ahkdhergy February 4, 2021 at 7:00 pm

    canadian pharmacy certified canada pharmacy online canadian drugstore canadian prescription drugstore

  7. netflix February 6, 2021 at 4:13 pm

    Nice blog post. I learn something a lot more difficult on different blog sites everyday. It will certainly constantly be promoting to review content from other authors as well as exercise a something from their shop. I?d prefer to utilize some with the material on my blog whether you don?t mind. Natually I?ll give you a link on your web blog. Many thanks for sharing. Caralie Uri Jacquetta

  8. torrent February 6, 2021 at 4:25 pm

    This content was super Thanks so much for creating it. Beverlee Filberto Ellery

  9. online February 6, 2021 at 4:26 pm

    maam kinakabahan talaga aq kasi almost a year from my last cs professional exam.. 2 DAYS NA LANG EXAM NA. God bless us all Adoree Tynan Whitehouse

  10. yabanci February 6, 2021 at 4:44 pm

    mam pa send naman po pdf reviewer hindi po kc ako matalino kaya gusto ko makapagstudy ng maaga bakasakali pong makapasa ako sa cs. tnx a lot Liliane Pat Loram

  11. filmkovasi February 6, 2021 at 6:41 pm

    Wonderful, what a webpage it is! This website gives useful information to us, keep it up. Muriel Marshal Kessel Juline Edouard Philis

  12. turkce February 6, 2021 at 10:57 pm

    I was extremely pleased to discover this great site. I want to to thank you for your time for this particularly wonderful read!! I definitely appreciated every bit of it and i also have you bookmarked to see new stuff on your web site. Ardisj Bond Susette

  13. yabanci February 10, 2021 at 6:44 am

    I was JUST thinking yesterday that I wished someone would put out a sheet pan Thanksgiving meal bc I am not up to more than that this year for celebrating with only my husband and three kids. THANKS! This looks amazing. Perla Erhart Helbonnas

  14. Donaldvic February 17, 2021 at 10:43 pm

    Very smooth paragraph

    free robux

  15. offshore software development February 21, 2021 at 8:30 pm

    Thanks designed for sharing such a good idea, paragraph is good, thats why i
    have read it entirely

  16. judi online February 25, 2021 at 10:26 pm

    Sweet blog! I found it while browsing on Yahoo News. Do you have any
    tips on how to get listed in Yahoo News? I’ve been trying for a while but I never
    seem to get there! Thank you

  17. daftar judi online terbaik February 27, 2021 at 5:27 am

    Wow, amazing blog layout! How long have you
    been blogging for? you made blogging look easy.
    The overall look of your site is wonderful, let
    alone the content!

  18. Numerologist in India March 13, 2021 at 11:45 am

    Thanks for every other informative blog. Τһe place
    еlse mаy Ӏ am gettіng that kind of information ԝritten Numerologist in India
    sսch an ideal way? I’ѵe а challenge thаt Ӏ’m jᥙst noѡ working
    on, аnd I’νe been at tһe look out for such info.

  19. prednisolone and croup March 20, 2021 at 7:22 am

    prednisolone ointment prednisolone 0.5 what is prednisolone used for in cats what is prednisolone sol 15mg/5ml and triamcinolone used together for

  20. synthroid and cytomel March 24, 2021 at 3:23 am

    synthroid direct.com synthroid 225 mcg symptoms of too much synthroid after thyroidectomy i skipped a synthroid dose because my tsh was too low. how long before the level drops?

  21. asmr https://0mniartist.tumblr.com April 10, 2021 at 10:52 am

    I all the time emailed this weblog post page to all my contacts, for the reason that if like to read it then my links will too.
    asmr 0mniartist

Leave a comment

Your email address will not be published. Required fields are marked *