Var and let keyword - Scope in Javascript

Поділитися
Вставка
  • Опубліковано 17 вер 2024
  • Welcome to a youtube channel dedicated to programming and coding related tutorials. We talk about tech, write code, discuss about cloud and devops. That’s what we do all day, all year. We roll out a lot of series and videos on our channel.
    All the learning resources such as code files, documentations, articles and community discussions are available on our website:
    chaicode.com/
    You can find our discord link, github link etc on the above website.
    Twitter/X link: x.com/hiteshdo...
    Discord link: hitesh.ai/discord
    Learn React with 10 projects: • Let's learn react from...
    Learn Docker: • A practical guide on D...
    Learn Kubernetes: • Complete Kubernetes Co...
    How does a browser works: • How does a browser wor...
    How nodejs works: • How node JS works | En...
    Learn Redux-toolkit: • Learn Redux Toolkit in...
    Learn NextJS: • Nextjs Full stack course
    Learn Typescript: • Why to learn Typescript
    Learn Javascript: • Welcome to new JavaScr...
    Learn React Native: • React Native Mastery: ...
    Learn Zustand: • React state management...
    Learn Golang: • How to get started wit...

КОМЕНТАРІ • 71

  • @amans6504
    @amans6504 3 роки тому +3

    if you're reading this, i got this after reading a ton of blogs
    It's very easy,
    1. var is functionally scoped ONLY
    2. let is block scoped
    3. a block is a code kept inside curly braces"{}". so basically a function, if statement or even random global curly braces are all blocks
    so basically, when u declare a function and declare a variable with var inside that function, the var is FUNCTIONALLY scoped and cannot be acessed by its parent(or in global scope) WHEREAS if you declare the same var inside an if statement, IT WILL easily be accessed in the parent scope because, var is functionally scoped ONLY
    whereas in the case of let, doesn't matter if you declare it inside a function or inside an if statement, in both the case it is not accessible in the parent scope, because function and if statement both are blocks afterall, and let is block scoped!
    basically just dont use var,use let and you'll be fine.

    • @vivek1692asd
      @vivek1692asd 3 роки тому

      Thanks for more clarification.

  • @pile333
    @pile333 6 років тому +2

    A Nobel prize for teaching should be instituted and it should go to Hitesh Choudhary. ^_^
    Thx.

  • @prabhatjena5833
    @prabhatjena5833 6 років тому +13

    very nice series..at rapid pace...if at the end of series one video comes chrome dev. tools for debugging Javascript..this will make a great js series...thnx..for tutorials..

  • @TheRicokilla
    @TheRicokilla 4 роки тому +1

    Ur explanation is good and clear. I was having a hell time with this subject during my course, until I found ur channel. Good job guys👍

  • @shubhamsharma-pu2sp
    @shubhamsharma-pu2sp 6 років тому +5

    Now I understand why you use LET

  • @AtoZDominations
    @AtoZDominations 5 років тому +2

    I don't understand, if we change iamGlobal inside the if block and call it outside the if block it should print 'somevalue', instead of superman. Why is it affecting the global variable outside the if block?

  • @SP-ty5zw
    @SP-ty5zw 6 років тому

    this is really like bahubali series for our code world.. amazing amazing , n+1 amazing, tq hitesh for this, hats off to u

  • @kushaldhakal3009
    @kushaldhakal3009 6 років тому +9

    By scope, I first thought of job opportunity, salary and something like that. Lol

  • @codingwithsam9194
    @codingwithsam9194 6 років тому +2

    if the if() is false the code block doesn't runs right then why am I facing a problem with this code???
    let jj = 'code'
    if (false) {
    var jj = 'code1'
    }
    console.log(jj);
    nd the error is:
    /Users/samcomputer/node js/practice.js:3
    var jj = 'code1'
    ^
    SyntaxError: Identifier 'jj' has already been declared
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
    let me know why?

    • @danishnazirpeer3422
      @danishnazirpeer3422 6 років тому

      Coding with sam you can't declare a variable twice...... In this case, u declared variable jj using let and then var

  • @sivathejeswarareddyk3149
    @sivathejeswarareddyk3149 6 років тому +2

    Love the series

  • @yashrajanshukla7790
    @yashrajanshukla7790 6 років тому

    Eagerly waited for your rain of Javascript.. And then you posted this video in morning ... Amazing. Supee amazing video

  • @mayurpandit8166
    @mayurpandit8166 3 роки тому

    Thanks sirji, most askable question in interview, now I am clear about lat and var declaration

  • @msdhaliwal
    @msdhaliwal 6 років тому

    very helpful for both beginners and experts

  • @ganeshkolase7203
    @ganeshkolase7203 4 роки тому

    Nice explanation with example sir.

  • @ronakkela
    @ronakkela 4 роки тому

    Huge respect for the content you are providing here..
    Been blessed after subscribing here😉😉

  • @SahilKumar-rw3sh
    @SahilKumar-rw3sh 2 роки тому

    nicely taught , sir

  • @Souravbaranwal
    @Souravbaranwal 5 років тому

    Hey Hitesh, I've seen some of your videos and it really helped. Thanks a lot !!

  • @vinitsharma7735
    @vinitsharma7735 5 років тому

    Hey Hitesh,
    Please let us know what are the changes done by you to save time and all.

  • @rajanchoudhary7432
    @rajanchoudhary7432 6 років тому

    very nicely explained. Keep Uploading

  • @alid2756
    @alid2756 6 років тому +3

    Hi Hatish, thank you for these amazing videos, how can I configure my integrated terminal in VScode to run my JS instead of windows cmd? thank you

    • @vishaldivekar09
      @vishaldivekar09 4 роки тому +1

      For windows: just change file association of .js file to node.exe
      1) Take VSCode
      2) Right click on the file in left pane
      3) Click "Reveal in explorer" from context menu
      4) Right click on the file -> Select "Open with" -> Select "Choose another program"
      5) Check box "Always use this app to open .js file"
      6) Click "More apps" -> "Look for another app in PC"
      7) Navigate to node.js installation directory.(Default C:\Program Files
      odejs
      ode.exe"
      8) Click "Open" and you can just see cmd flashing
      9) Restart vscode and open the file -> Terminal Menu -> "Run active file".

    • @alid2756
      @alid2756 4 роки тому

      @@vishaldivekar09 Thank you

  • @Bajrang.707
    @Bajrang.707 5 років тому +2

    Thanks for this :)

  • @thedeepeffect_
    @thedeepeffect_ 5 років тому

    if we look at the bright side of this var problem is that....if we want some variable to be defined in some conditions only and if that condition is true.......we will use that variable in our program for further use.....else we won't use that variable.........
    PS:Correct me if i'm wrong :)

  • @ashishpanjwani457
    @ashishpanjwani457 6 років тому

    one of my fav playlist

  • @imaginarytechinhindi5507
    @imaginarytechinhindi5507 6 років тому +2

    'let' keyword is used to declare constants in Swift...

    • @RockDavid
      @RockDavid 5 років тому +1

      Well good thing this isn't Swift, nor your comment has anything to do with Javascript..
      Good times, ohh and the world is flat

  • @deepak_dalvi
    @deepak_dalvi 6 років тому

    Hiee..Thank you making this kind quility videos for JavaScript.
    I want to know there is any way to pass variable from javascript to PHP

  • @gauravk4050
    @gauravk4050 6 років тому

    #unstoppable
    love this playlist..😻

  • @viralpatva7093
    @viralpatva7093 6 років тому

    Very helpful tutorial.

  • @adin954
    @adin954 4 роки тому

    yo, so with the value part. i dont know if im doing something wrong but this is the code
    let iAmGlobal = "someValue"
    if (true){
    let iamLocal = "someMoreValue";
    iAmGlobal = "superman";
    console.log(iAmGlobal);
    console.log(iamLocal);
    }
    console.log(iamLocal);
    console.log(iAmGlobal);
    Output:node scope.js
    someValue
    someMoreValue
    each time please answer me.

  • @Devank12
    @Devank12 6 років тому

    thank you sir,how to add terminal in the VS please make a video

  • @rajeshkumarsahoo
    @rajeshkumarsahoo 6 років тому

    Awesome clarification

  • @koustubhmuktibodh4901
    @koustubhmuktibodh4901 5 років тому

    Sir great explanation !

  • @petrospetrosyan1414
    @petrospetrosyan1414 4 роки тому +2

    What's the difference between using “let” and “var”?

  • @sachintiwari5871
    @sachintiwari5871 6 років тому

    Great sir.....
    #Unstoppable....

  • @srikanthracharla420
    @srikanthracharla420 4 роки тому +1

    let iAmGlobal = "someValue";
    if (true) {
    var iAmLocal = "someMoreValue";
    //let iAmLocal = "someMoreValue";
    iAmGlobal = "superman";
    console.log(iAmGlobal);
    console.log(iAmLocal);
    }
    //console.log(iAmLocal);
    console.log(iAmGlobal);

  • @jincypaul05
    @jincypaul05 5 років тому

    Sir, How did you access the terminal inside VSCode. Can you please answer?
    Thank you very much for the amazing JavaScript series!!!!!!

    • @neelakshsharma4936
      @neelakshsharma4936 5 років тому

      Just go to the terminal option displayed in the top and select New Terminal. Or simply press ctrl + shift + '

  • @niharikakuravii7420
    @niharikakuravii7420 6 років тому

    great series sir

  • @mishenthakshana3407
    @mishenthakshana3407 2 роки тому

    Thanks

  • @harshitawasthi2420
    @harshitawasthi2420 5 років тому

    Sir If i have same name variable in global as well as local scope and if i access that variable inside local scope it will give preference to local variable . So how could i use that global variable inside local scope ?

  • @manindersohal5861
    @manindersohal5861 4 роки тому

    if 'iAmGlobal' was reassigned a value inside if block, shouldnt its scope be inside block only. Why does its value changes outside the block later on? Do kindly reply!

  • @codingwithsam9194
    @codingwithsam9194 6 років тому

    Ok sir nice but please upload your live stream today. I have plenty of questions to ask you...

  • @amishaa9555
    @amishaa9555 6 років тому

    i am so enthusiastic that i will definitely try to work harder and smarter and master the course

  • @nayandas-ld4zp
    @nayandas-ld4zp 5 років тому

    Great ...!

  • @sriharshajaddu3426
    @sriharshajaddu3426 6 років тому

    Phases of web development for learning 1st html or css or js please reply would be helpful??!

    • @HiteshCodeLab
      @HiteshCodeLab  6 років тому

      Already did an extensive series on front end at my website, learncodeonline.in

  • @sushilknm06
    @sushilknm06 5 років тому

    your every series in mac os ,is there something for windows?

    • @HiteshCodeLab
      @HiteshCodeLab  5 років тому

      Written in VSCode, executed in browser. All tools used are cross platform.

  • @polditeref5162
    @polditeref5162 4 роки тому

    It would have been much good if "const" is also there.

  • @papachoudhary5482
    @papachoudhary5482 6 років тому

    Nice

  • @debasishdutta9073
    @debasishdutta9073 5 років тому

    Amazing..
    But i have a question for you.......
    ..1)Who r you?
    2)What is your main job?
    3)How many programming language do you know very well?

  • @AbhishekGupta-xh5si
    @AbhishekGupta-xh5si 6 років тому

    how to do tab on typing 'log' so it becomes the whole thing?

  • @marksahlgreen9584
    @marksahlgreen9584 4 роки тому

    when I am writing if statements in vscode and I fill in the condition, it doesn't autocomplete variable names. Anyone know how I can fix this?

  • @sayantaniguha8519
    @sayantaniguha8519 3 роки тому

    for(var i=1;i

  • @bikkyjbn4065
    @bikkyjbn4065 6 років тому

    Bro need your help in my affiliated flipkart dashboard shoing migrate to new api version .so how to solve this .hope u will solve my problem .please reply me bro... i m waiting ..

  • @amitojsinghahuja4330
    @amitojsinghahuja4330 6 років тому

    thankyou!!!

  • @bhanurulz
    @bhanurulz 6 років тому

    lot of respect :)

  • @ssshivan
    @ssshivan 3 роки тому

    IF I OPEN THE TERMINAL IT SAYS 101 error crash and whole thing closes andthe folder also got closed
    pls help me how to fix it

  • @roostr14
    @roostr14 4 роки тому

    But this concept does not working in function why this so
    Can you explain.

  • @AreAreTv
    @AreAreTv 5 років тому

    Hi, how can email you because i have a project and need to create a special calculator, i will pay. Please contact me privately. Thank you.

  • @yashmishra5796
    @yashmishra5796 4 роки тому

    hitesh how can i take input from user

  • @vimalsingh3544
    @vimalsingh3544 6 років тому

    sir pls tell how we can give user input in javascript

  • @yashshende2786
    @yashshende2786 6 років тому +2

    first

  • @Fullstackdev18
    @Fullstackdev18 4 роки тому

    Nice try. :)

  • @kylenakamura4353
    @kylenakamura4353 5 років тому

    This "wideo" should have only been 2 minutes long. Way too in depth.

  • @rkrk8146
    @rkrk8146 3 роки тому

    Confusing