Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Anil Kumar Khandei
Anil Kumar Khandei

Posted on

     

JS Var vs Let difference

The difference betweenvar andlet in JavaScript is block scope.

var

When avar is declared outside a block scope, and then re declared inside a block after the first declaration. The value of thevar gets changed after the block scope is over.

//first declaration of var xvarx=10;{//second declaration of var xvarx=5;alert(x);//prints 5}alert(x);//prints 5
Enter fullscreen modeExit fullscreen mode

let

However by usinglet keyword to declare a variable. the value of the variable doesn't get changed by the subsequent re declaration inside the block.

//first declaration of let var ylety=15;{//second declaration of let var ylety=90;alert(y);//prints 90}alert(y);//prints 15
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Fullstack Development, Azure, React, JS, Donetcore, SQL, XML, XSLT
  • Location
    California
  • Education
    Bachelor Degree in Computer Science
  • Work
    Technical Lead at Mindtree Ltd.
  • Joined

More fromAnil Kumar Khandei

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp