{"id":1119,"date":"2023-12-18T21:00:00","date_gmt":"2023-12-18T21:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=1119"},"modified":"2023-12-23T11:18:24","modified_gmt":"2023-12-23T11:18:24","slug":"let-and-var-difference-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/let-and-var-difference-javascript\/","title":{"rendered":"Differences Between let and var in JavaScript Explained"},"content":{"rendered":"\n<p>While declaring variables you might get confused between the choice of two keywords in JavaScript: let and var. They might look similar at first, but both of them are different in terms of scope and behavior. In this article, we will explore the differences between let and var keywords in JavaScript, along with many examples.<\/p>\n\n\n\n<p>Before moving forward, let&#8217;s revise both of them first!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is the var keyword?<\/strong><\/h2>\n\n\n\n<p>When we create a variable using the var keyword in JavaScript, it makes the variable to be function-scoped, i.e. the variable can be accessed throughout the entire function in which it was declared. Earlier the var keyword was the primary way of declaring variables. <\/p>\n\n\n\n<p>Let\u2019s see how we can use the var keyword:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">function example() {\n  var x = 5;\n  console.log(x);\n}<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>5<\/code><\/pre>\n\n\n\n<p>In the example above, the variable x is declared using var within the example function. Since var is function-scoped, we can access it anywhere inside the function.<\/p>\n\n\n\n<p>Additionally, variables declared with var can also be redeclared within the same scope. This can result in potential issues and bugs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is the let keyword?<\/strong><\/h2>\n\n\n\n<p>When we use the let keyword to declare a variable in JavaScript, it makes the variable block-scoped, i.e. it can be accessed only within the block of code in which they are declared.<\/p>\n\n\n\n<p>The let keyword was introduced in JavaScri[t ES6 in 2015. It can be seen as a more modern alternative to var. <\/p>\n\n\n\n<p>Let\u2019s see how we can use the let keyword:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">function example() {\n  let message = &quot;Hello, world!&quot;;\n  console.log(message); \n  if (true) {\n    let message = &quot;Goodbye, world!&quot;;\n    console.log(message); \n  }\n  console.log(message);\n}<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Hello, world!\nGoodbye, world!\nHello, world!\n<\/code><\/pre>\n\n\n\n<p>In the above example, we declared the variable message with let within the example function. The first console.log statement within the if block will output &#8220;Goodbye, world!&#8221;, as the message is block-scoped and limited to the block in which it is declared.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Difference between let and var in JavaScript<\/strong><\/h2>\n\n\n\n<p>Now that you have a basic idea about both of them, let&#8217;s discuss the key differences between let and var in JavaScript:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Scope<\/strong><\/h3>\n\n\n\n<p><strong>The main difference between let and var in JavaScript lies in their scope. The variables declared with var are function-scoped, while the let variables are block-scoped.\u00a0<\/strong><\/p>\n\n\n\n<p>This means that when we declare the variable using var, they can be accessed throughout the entire function in which they are declared, as well as any nested functions. On the other hand, we can only access the let variables within the block of code in which they are declared.<\/p>\n\n\n\n<p>Let\u2019s see an example to compare them.<\/p>\n\n\n\n<p><strong>Variables using var:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">function example() {\n  var x = 5;\n  if (true) {\n    var x = 10;\n    console.log(x);\n  }\n  console.log(x);\n}<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>10\n10\n<\/code><\/pre>\n\n\n\n<p><strong>Variables using let:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">function example() {\n  let y = 5;\n  if (true) {\n    let y = 10;\n    console.log(y);\n  }\n  console.log(y);\n}<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>10\n5\n<\/code><\/pre>\n\n\n\n<p>In the first example, the var variable x is accessible both inside and outside the if block, leading to a change in its value. However, in the second example, the let variable y is block-scoped, so the value inside the if block does not affect the value outside of it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Redeclaration<\/strong><\/h3>\n\n\n\n<p>Another important difference lies in their behavior to redeclaration. We can redeclare a variable declared using var within the same scope which can cause unexpected results and bugs.\u00a0For example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">var a = 5;\nvar a = 10;\nconsole.log(a);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>10<\/code><\/pre>\n\n\n\n<p>In the example above, we initially declared the var variable a with a value of 5. However, we have redeclared it with a value of 10 within the same scope, resulting in a final output of 10.<\/p>\n\n\n\n<p>On the other hand, we can not redeclare the variable using let within the same scope. If we attempt to redeclare a let variable, we will encounter an error. For example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">let b = 5;\nlet b = 10; <\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Error: Identifier 'b' has already been declared<\/code><\/pre>\n\n\n\n<p>In this example, when we tried to redeclare the variable b again with value 10, it showed the error that \u2018b\u2019 has already been declared.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Hoisting<\/strong><\/h3>\n\n\n\n<p>Hoisting is a JavaScript behavior in which variable and function declarations are moved to the top of their respective scopes during the compilation phase.\u00a0<\/p>\n\n\n\n<p><strong>There is a difference in hoisting behavior between the variables declared using let and var. The variables declared using var are hoisted to the top of their scope, which allows us to access and use a variable before it is declared.<\/strong><\/p>\n\n\n\n<p>For example,If we try to access the variable a before it is declared, it will print undefined which is the initial value of it:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">console.log(a);\nvar a = 5;\nconsole.log(a);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>undefined \n5\n<\/code><\/pre>\n\n\n\n<p>While, when we try to access the let variables, it will result in a ReferenceError as they are not hoisted. Let\u2019s see it with another example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">console.log(b); \nlet b = 5;\nconsole.log(b); <\/pre><\/div>\n\n\n\n<p><strong>Output:&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>ReferenceError: Cannot access 'b' before initialization\n5<\/code><\/pre>\n\n\n\n<p>In this example, the let variable b is not hoisted, and trying to access it before initialization leads to a ReferenceError.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Key Differences Between var and let Keywords<\/strong><\/h2>\n\n\n\n<p>Let&#8217;s now summarize the key differences:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular has-medium-font-size\"><table><thead><tr><th><strong>Basis<\/strong><\/th><th><strong>var keyword<\/strong><\/th><th><strong>let keyword<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>Scope<\/strong><\/td><td>Function-scoped<\/td><td>Block-scoped<\/td><\/tr><tr><td><strong>Re-declaration<\/strong><\/td><td>Allowed<\/td><td>Non Allowed within the same space<\/td><\/tr><tr><td><strong>Initialization<\/strong><\/td><td>Can be declared without initialization<\/td><td>Needs to be initialized before use<\/td><\/tr><tr><td><strong>Use in Loops<\/strong><\/td><td>Shared across the entire function&nbsp;<\/td><td>Limited to the block it\u2019s declared in<\/td><\/tr><tr><td><strong>Hoisting<\/strong><\/td><td>Hosited ( moved to the top of its scope )<\/td><td>Not hoisted ( remains in the temporal dead zone until declaration )<\/td><\/tr><tr><td><strong>JavaScript version<\/strong><\/td><td>It is an ECMAScript1 feature<\/td><td>Introduced in ECMAScript6 ES6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In conclusion, understanding the differences between let and var is crucial for effective variable declaration in JavaScript. While var has been the traditional way of declaring variables, let provides block scope and stricter behavior, making it the preferred choice in modern JavaScript development. By utilizing let and var appropriately, we can write more robust and maintainable code. If you still have some doubts, get our <a href=\"https:\/\/favtutor.com\/programming-help\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/programming-help\">programming assignment help online<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understand the key differences between let and var keywords in JavaScript, based on scope, redeclaration, and hoisting.<\/p>\n","protected":false},"author":9,"featured_media":1121,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[9],"tags":[11],"class_list":["post-1119","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=1119"}],"version-history":[{"count":5,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1119\/revisions"}],"predecessor-version":[{"id":1154,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1119\/revisions\/1154"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/1121"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=1119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=1119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=1119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}