Var and let keyword - Scope in Javascript
HTML-код
- Опубликовано: 11 сен 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...
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.
Thanks for more clarification.
A Nobel prize for teaching should be instituted and it should go to Hitesh Choudhary. ^_^
Thx.
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..
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👍
this is really like bahubali series for our code world.. amazing amazing , n+1 amazing, tq hitesh for this, hats off to u
Eagerly waited for your rain of Javascript.. And then you posted this video in morning ... Amazing. Supee amazing video
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?
Now I understand why you use LET
Love the series
By scope, I first thought of job opportunity, salary and something like that. Lol
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?
Coding with sam you can't declare a variable twice...... In this case, u declared variable jj using let and then var
Thanks sirji, most askable question in interview, now I am clear about lat and var declaration
Huge respect for the content you are providing here..
Been blessed after subscribing here😉😉
Hey Hitesh, I've seen some of your videos and it really helped. Thanks a lot !!
Nice explanation with example sir.
nicely taught , sir
very helpful for both beginners and experts
Thanks for this :)
very nicely explained. Keep Uploading
Hey Hitesh,
Please let us know what are the changes done by you to save time and all.
one of my fav playlist
Very helpful tutorial.
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
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".
@@vishaldivekar09 Thank you
#unstoppable
love this playlist..😻
Sir great explanation !
Awesome clarification
Great sir.....
#Unstoppable....
Thankyou sir
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 :)
Thanks
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.
great series sir
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!
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 ?
'let' keyword is used to declare constants in Swift...
Well good thing this isn't Swift, nor your comment has anything to do with Javascript..
Good times, ohh and the world is flat
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
Great ...!
Sir, How did you access the terminal inside VSCode. Can you please answer?
Thank you very much for the amazing JavaScript series!!!!!!
Just go to the terminal option displayed in the top and select New Terminal. Or simply press ctrl + shift + '
i am so enthusiastic that i will definitely try to work harder and smarter and master the course
Ok sir nice but please upload your live stream today. I have plenty of questions to ask you...
lot of respect :)
thank you sir,how to add terminal in the VS please make a video
thankyou!!!
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
But this concept does not working in function why this so
Can you explain.
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.
What's the difference between using “let” and “var”?
how to do tab on typing 'log' so it becomes the whole thing?
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?
hitesh how can i take input from user
your every series in mac os ,is there something for windows?
Written in VSCode, executed in browser. All tools used are cross platform.
Phases of web development for learning 1st html or css or js please reply would be helpful??!
Already did an extensive series on front end at my website, learncodeonline.in
Nice
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 ..
for(var i=1;i
first
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?
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);
sir pls tell how we can give user input in javascript
It would have been much good if "const" is also there.
Nice try. :)
Confusing
This "wideo" should have only been 2 minutes long. Way too in depth.