{"id":5973,"date":"2024-12-17T08:58:41","date_gmt":"2024-12-17T08:58:41","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=5973"},"modified":"2025-09-03T05:51:17","modified_gmt":"2025-09-03T05:51:17","slug":"difference-between-let-and-var-in-javascript","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/","title":{"rendered":"Difference Between Let and Var in JavaScript"},"content":{"rendered":"\n<p>Typically, JavaScript developers use var keyword to define variables. But that is not the only way to declare and define variables. In fact, starting ES6 in 2015, developers can also use let and const keywords to define variables. But many developers are not sure how they work, how they differ from var keyword and when to use them. In this article, we will learn about the difference between let and var in JavaScript.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#What_is_Var_Keyword\" >What is Var Keyword<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#What_is_Let_Keyword\" >What is Let Keyword<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#Difference_Between_Let_and_Var_in_JavaScript\" >Difference Between Let and Var in JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#Scoping\" >Scoping<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#Global_Object_Property_Outside_Function\" >Global Object Property \/ Outside Function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#Inside_Function\" >Inside Function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#Inside_Block\" >Inside Block<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#Inside_Loop\" >Inside Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#Loops_with_closures\" >Loops with closures<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#Hoisting\" >Hoisting<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#Re-declaration\" >Re-declaration<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#Temporal_Dead_Zone\" >Temporal Dead Zone<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#Comparison_Between_Let_vs_Var\" >Comparison Between Let vs Var<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#FAQ\" >FAQ<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_Var_Keyword\"><\/span>What is Var Keyword<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Since early days, JavaScript has allowed us to define variables using var keyword. Once you define a variable using var keyword, its scope is global and is accessible throughout the JavaScript file. Often, it is also used within a function for function scoping of variables. So if you happen to redeclare this variable anywhere inside or outside function, then it will be redefined everywhere else. This poses a problem especially in large JavaScript files which may access a variable from multiple points. That is where let keyword comes in.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_Let_Keyword\"><\/span>What is Let Keyword<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let keyword allows you to declare block-scoped variables that can be re-assigned. But they must be declared before use, else they will give error. Also they cannot be redeclared inside the same block.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Difference_Between_Let_and_Var_in_JavaScript\"><\/span>Difference Between Let and Var in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are several key differences between let and var keywords in JavaScript. Let us look at them one by one, in detail.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Scoping\"><\/span>Scoping<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>One of the most important differences between let and var is their scoping rules. Var declared variables are scoped to the function where they are defined. Let declared variables are scoped to the immediate code block, that is, within curly braces &#8216;{&#8230;}&#8217;. The reason why let and const keywords were introduced is to support block scoping, as opposed to function or global scoping available with var keyword.<\/p>\n\n\n\n<p>Here is an example to illustrate their difference.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function a(){<br>   var b=1;<br>   let c=2;<br>   console.log(b,c); \/\/ output is 1 2<br><br>{<br>  var d=3;<br>  let e=4;<br>  console.log(d,e); \/\/ output is 3 4<br>}<br>  <br>console.log(d);  \/\/ output is 3<br>console.log(e);  \/\/ ReferenceError<br><br>}<br><br>a()<\/pre>\n\n\n\n<p>In the above code, we defined a function a(). In it, we first defined variables b using var keyword and c using let keyword. They are both accessible inside the function scope. Then we create a code block inside the function. In it, we define variable d using var keyword and e using let keyword. They are both accessible within the code block. But when we try to access them outside the code block, variable d is accessible but variable e gives an error. This shows that let keyword has block scope and var keyword has function scope.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Global_Object_Property_Outside_Function\"><\/span>Global Object Property \/ Outside Function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You will also see that both var and let keywords can be used to create globally scoped variables, but the variables declared using let will not be a part of the global object. This is a very subtle difference. Here is an example to illustrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var a = 1; \/\/ globally scoped<br>let b = 2; \/\/ globally scoped but not part of the global object<br><br>console.log(window.a); \/\/ 1<br>console.log(window.b); \/\/ undefined<\/pre>\n\n\n\n<p>In the above code, we declare both variables a and b as global variables, using var and let keywords respectively. But only variable a is accessible using global object property window.a.<\/p>\n\n\n\n<p>Similarly, you cannot access globally scoped variables declared using let keyword, with <em>this<\/em> object either.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var a=1;<br>let b=2;<br><br>console.log(a); \/\/ 1<br>console.log(b); \/\/ 2<br><br>console.log(this.a); \/\/ 1<br>console.log(this.b); \/\/ undefined<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Inside_Function\"><\/span>Inside Function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Variables declared using var and let that are both inside a function but outside a code block, have the same scope and behave in a similar manner. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function test(){<br>  var a=1;<br>  var b=2;<br><br>  console.log(a);<br>  console.log(b);<br><br>}<br><br>test(); \/\/ output is 1 2<br>console.log(a);  \/\/ ReferenceError: a is not defined<br>console.log(b);  \/\/ ReferenceError: b is not defined<\/pre>\n\n\n\n<p>In the above code, both variables a and b are defined inside the function but not within any code block {&#8230;}. So they are both inaccessible outside the function and throw ReferenceError when we try to access either of them outside the function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Inside_Block\"><\/span>Inside Block<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this case, a variable declared using let keyword cannot be accessed outside the block. On the other hand, a variable declared using var keyword is accessible even outside the block.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{<br> var a=1;<br> let b=2;<br>}<br> <br>console.log(a); \/\/ output is 1<br>console.log(b); \/\/ ReferenceError: b is not defined<\/pre>\n\n\n\n<p>In the above code, when we try to access variable b declared using let keyword, it throws a ReferenceError.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Inside_Loop\"><\/span>Inside Loop<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Similarly, variables declared using let keyword in a loop are accessible only inside the loop. On the other hand, variables declared using var keyword are accessible outside also.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for (var i = 0; i &lt; 3; i++) {<br>  var j = i * 2;<br>}<br>console.log(i); \/\/ 3<br>console.log(j); \/\/ 4<br><br>for (let k = 0; k &lt; 3; k++) {<br>  let l = k * 2;<br>}<br>console.log(k); \/\/ ReferenceError: k is not defined<br>console.log(l); \/\/ ReferenceError: l is not defined<\/pre>\n\n\n\n<p>In the above code, neither of the two variable l or k, declared using let keyword, in the for loop, are accessible outside the loop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Loops_with_closures\"><\/span>Loops with closures<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>JavaScript Closures are combination of function with its surrounding state information (generally parent function) even after the parent has stopped execution. This allows you to implement state persistence in functions. When you use let keyword in a loop, in each iteration, you get a new variable. This is not the case when using var keyword. So you can safely use let keyword in closures.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ output is 3 3 3<br>for (var i = 0; i &lt; 3; i++) {<br>  setTimeout(() =&gt; console.log(i), 0);<br>}<br><br>\/\/ output is 0 1 2<br>for (let j = 0; j &lt; 3; j++) {<br>  setTimeout(() =&gt; console.log(j), 0);<br>}<\/pre>\n\n\n\n<p>In the above code, when we use let keyword, the for loop will display the counter values correctly. This indicates that let keyword is suitable for closures. But when we use var keyword, then each iteration will generate a new variable in every iteration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Hoisting\"><\/span>Hoisting<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Hoisting is the default behavior of JavaScript whereby all variable declarations are moved to the top of present scope. This can be to the top of the script or function where they are defined. This allows you to access variables even before they are explicitly initialized. <\/p>\n\n\n\n<p>Variables declared using var keyword are hoisted as well as initialized. Their value will be undefined if they are accessed before their initialization statement is executed. But your code will not throw an error and terminate execution.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function test() {<br>  console.log(a); \/\/ undefined<br>  var a = 1;<br>  console.log(a); \/\/ 1<br>}<br><br>test();<\/pre>\n\n\n\n<p>In the above code, we declare variable a using var keyword. We display its output before as well after initialization. The first time it returns undefined value and the second time it displays initialized value.<\/p>\n\n\n\n<p>When you declare variable using let keyword, then it is hoisted but not initialized until its initialization is actually executed. In this case, if you access them before initialization, then it will throw a ReferenceError and stop execution.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function test() {<br>  console.log(a); \/\/ ReferenceError: a is not defined<br>  var a = 1;<br>  console.log(a); \/\/ 1<br>}<br><br>test();<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Re-declaration\"><\/span>Re-declaration<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can always re-declare a variable that was declared using var, within the same scope. But you cannot re-declare a variable that was declared using let, within the same scope. It will give a SyntaxError. <\/p>\n\n\n\n<p>You cannot re-declare a variable that was declared using var keyword, using let keyword. It will also give SyntaxError. Similarly, you cannot re-declare a variable that was declared using let keyword, using var keyword. It will give SyntaxError.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var a;<br>var a; \/\/ No error or warning<br><br>let b;<br>let b; \/\/ SyntaxError: Identifier 'b' has already been declared<br><br>var c;<br>let c; \/\/ SyntaxError: Identifier 'c' has already been declared<br><br>let d;<br>var d; \/\/ SyntaxError: Identifier 'd' has already been declared<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Temporal_Dead_Zone\"><\/span>Temporal Dead Zone<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In JavaScript, a temporal dead zone is an area of code where the variable exists but is not accessible unless it is initialized. Due to this feature, variables declared using let keyword cannot be accessed before they are declared. Here is an example to demonstrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">console.log(a); \/\/ undefined<br>var a = 1;<br>console.log(b); \/\/ ReferenceError: b is not defined<br>let b = 2;<\/pre>\n\n\n\n<p>In the above code, we have two variables a and b declared using var and let keywords respectively. They are both accessed before they are initialized. The variable declared using var keyword returns undefined result whereas the variable defined using let keyword will throw a ReferenceError.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Comparison_Between_Let_vs_Var\"><\/span>Comparison Between Let vs Var<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now that we have a good understanding of how var and let keywords work, let us quickly go through the key differences between them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong>let<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>var<\/strong><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">syntax: let variable_name = value<\/td><td class=\"has-text-align-center\" data-align=\"center\">syntax: var variable_name = value<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Accessible within the code block {&#8230;} where they are declared<\/td><td class=\"has-text-align-center\" data-align=\"center\">Accessible within the function where they are declared, or globally if declared outside all functions.<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Cannot be redeclared within the same scope<\/td><td class=\"has-text-align-center\" data-align=\"center\">Can be redeclared within the same function scope. <\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Hoisted but not initialized. Throws ReferenceError if accessed before initialization.<\/td><td class=\"has-text-align-center\" data-align=\"center\">Hoisted and initialized by default. Will return undefined value if accessed before initialization statement is executed.<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Available since ECMA Script 6<\/td><td class=\"has-text-align-center\" data-align=\"center\">Available since ECMA Script 1, since beginning.<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Supported by most modern web browsers<\/td><td class=\"has-text-align-center\" data-align=\"center\">Supported by all web browsers<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Can be used with JavaScript closures<\/td><td class=\"has-text-align-center\" data-align=\"center\">Difficult to use with JavaScript closures<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Not part of global object property, even if you declare let variables globally.<\/td><td class=\"has-text-align-center\" data-align=\"center\">Part of global object property, if you declare var variable globally.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt about the detailed differences between let and var keywords in JavaScript. You can use either of them depending on your situation. Variables declared using let keyword are accessible within the scope of code block and cannot be re-declared within the same block. Variables declared using var keyword are accessible within their function block or global space. They can be re-declared within the same function or global scope.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQ\"><\/span>FAQ<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>1. Are var and let keywords interchangeable?<\/strong><\/p>\n\n\n\n<p>No. var keyword should be used for function scoping and let keyword should be used for block scoping of variables.<\/p>\n\n\n\n<p><strong>2. Can I re-declare a var declared variable as let variable? Or vice versa?<\/strong><\/p>\n\n\n\n<p>No. A var variable can be re-declared as var variable. If you re-declare a var variable as let variable or vice versa, you will get SyntaxError.<\/p>\n\n\n\n<p><strong>3. What is the advantage of let over var?<\/strong><\/p>\n\n\n\n<p>Let keyword allows you block specific scoping. It allows you to prevent accidental creation of global scope variables, since they have more restrictive scope than var variables.<\/p>\n\n\n\n<p><strong>4. What is the advantage of var over let?<\/strong><\/p>\n\n\n\n<p>Variables declared using var keyword are available throughout the function where they are defined. If you define them as global variables, then they are also available in global scope.<\/p>\n\n\n\n<p><strong>5. What are the disadvantages of let keyword?<\/strong><\/p>\n\n\n\n<p>Let keyword has limited scope compared to those declared using var. Also, they tend to give ReferenceError and SyntaxError if they are not used properly. Variables declared using var may very often return undefined value and continue execution, instead of throwing an error and terminating execution.<\/p>\n\n\n\n<p><strong>6. What are the disadvantages of var keyword?<\/strong><\/p>\n\n\n\n<p>Since var keyword has a wide scope, it can cause problems especially in large scripts. Also, since they can be easily re-declared, it can have unpredictable values if it is being modified by multiple parts of your script.<\/p>\n\n\n\n<p><strong>7. When to use let keyword?<\/strong><\/p>\n\n\n\n<p>Let keyword should be used if you want to assign block level variable scope, that can be reassigned multiple times. Secondly, they are great for using in closures and preserving function state.<\/p>\n\n\n\n<p><strong>8. When to use var keyword?<\/strong><\/p>\n\n\n\n<p>Var keyword is widely used by JavaScript developers. It can be used if you want your variables to have a global scope or function scope. They are also great if you want to re-declare them.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/\">How to Add Table Row in jQuery<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-disable-enable-input-in-jquery\/\">How to Disable\/Enable Input in jQuery<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes web developers wonder the difference between let and var in JavaScript. Here is a detailed comparison of let vs var.<\/p>\n","protected":false},"author":1,"featured_media":6005,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[295],"tags":[354],"class_list":["post-5973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-let-vs-var"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Difference Between Let and Var in JavaScript - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Sometimes web developers wonder the difference between let and var in JavaScript. Here is a detailed comparison of let vs var.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Difference Between Let and Var in JavaScript - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Sometimes web developers wonder the difference between let and var in JavaScript. Here is a detailed comparison of let vs var.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-17T08:58:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-03T05:51:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/difference-between-let-and-var.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"288\" \/>\n\t<meta property=\"og:image:height\" content=\"192\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"Difference Between Let and Var in JavaScript\",\"datePublished\":\"2024-12-17T08:58:41+00:00\",\"dateModified\":\"2025-09-03T05:51:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/\"},\"wordCount\":1692,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/difference-between-let-and-var.jpg?fit=288%2C192&ssl=1\",\"keywords\":[\"let vs var\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/\",\"name\":\"Difference Between Let and Var in JavaScript - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/difference-between-let-and-var.jpg?fit=288%2C192&ssl=1\",\"datePublished\":\"2024-12-17T08:58:41+00:00\",\"dateModified\":\"2025-09-03T05:51:17+00:00\",\"author\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Sometimes web developers wonder the difference between let and var in JavaScript. Here is a detailed comparison of let vs var.\",\"breadcrumb\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/difference-between-let-and-var.jpg?fit=288%2C192&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/difference-between-let-and-var.jpg?fit=288%2C192&ssl=1\",\"width\":288,\"height\":192,\"caption\":\"difference between let and var\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ubiq.co\/tech-blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Difference Between Let and Var in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/\"],\"url\":\"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Difference Between Let and Var in JavaScript - Ubiq BI","description":"Sometimes web developers wonder the difference between let and var in JavaScript. Here is a detailed comparison of let vs var.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Difference Between Let and Var in JavaScript - Ubiq BI","og_description":"Sometimes web developers wonder the difference between let and var in JavaScript. Here is a detailed comparison of let vs var.","og_url":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2024-12-17T08:58:41+00:00","article_modified_time":"2025-09-03T05:51:17+00:00","og_image":[{"width":288,"height":192,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/difference-between-let-and-var.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"Difference Between Let and Var in JavaScript","datePublished":"2024-12-17T08:58:41+00:00","dateModified":"2025-09-03T05:51:17+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/"},"wordCount":1692,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/difference-between-let-and-var.jpg?fit=288%2C192&ssl=1","keywords":["let vs var"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/","url":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/","name":"Difference Between Let and Var in JavaScript - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/difference-between-let-and-var.jpg?fit=288%2C192&ssl=1","datePublished":"2024-12-17T08:58:41+00:00","dateModified":"2025-09-03T05:51:17+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Sometimes web developers wonder the difference between let and var in JavaScript. Here is a detailed comparison of let vs var.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/difference-between-let-and-var.jpg?fit=288%2C192&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/difference-between-let-and-var.jpg?fit=288%2C192&ssl=1","width":288,"height":192,"caption":"difference between let and var"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/difference-between-let-and-var-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"Difference Between Let and Var in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/difference-between-let-and-var.jpg?fit=288%2C192&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1yl","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=5973"}],"version-history":[{"count":48,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5973\/revisions"}],"predecessor-version":[{"id":9452,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5973\/revisions\/9452"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/6005"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=5973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=5973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=5973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}