{"id":752,"date":"2022-07-06T06:02:35","date_gmt":"2022-07-06T06:02:35","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=752"},"modified":"2022-07-06T06:02:38","modified_gmt":"2022-07-06T06:02:38","slug":"let-keyword-%e2%86%92-for-a-variable-with-definite-scope","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/let-keyword-%e2%86%92-for-a-variable-with-definite-scope\/","title":{"rendered":"let keyword in javascript"},"content":{"rendered":"<p style=\"text-align: center;\"><strong>let keyword \u2192 for a variable with definite scope<\/strong><\/p>\n<p>The thing about creating the variable with the var keyword is that the variable created with the var keyword has a global scope, or a function scope(global means that the variable can be used ANYWHERE in the program, and function scope means that the variable can be used inside the function)<\/p>\n<p>But, when you create a variable using the let keyword, it has a block scope, which means that it is created for a particular block.<\/p>\n<p>It is likely that wherever you see some curly brackets {}, it is a block, so you can say that those variables are just created for that block and they won&#8217;t be available or known outside the block.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-753 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/let-\u2192-for-a-variable-with-a-definite-scope.jpg\" alt=\"\" width=\"829\" height=\"351\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/let-\u2192-for-a-variable-with-a-definite-scope.jpg 829w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/let-\u2192-for-a-variable-with-a-definite-scope-300x127.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/let-\u2192-for-a-variable-with-a-definite-scope-768x325.jpg 768w\" sizes=\"auto, (max-width: 829px) 100vw, 829px\" \/><\/p>\n<p>Output \u2192<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-754 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/let-\u2192-for-a-variable-with-a-definite-scope-1.jpg\" alt=\"\" width=\"576\" height=\"437\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/let-\u2192-for-a-variable-with-a-definite-scope-1.jpg 576w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/let-\u2192-for-a-variable-with-a-definite-scope-1-300x228.jpg 300w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><br \/>\nAs you can see, we had declared a variable using the keyword let, inside some block. Now, we can access it inside the block, but not outside the block. This is the block scope. So, when we try to use it outside that block, we just ran into an error. On the other hand, if you replace the let with var inside the block, then we would be able to use the variable outside the block too. You can try doing it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>let keyword \u2192 for a variable with definite scope The thing about creating the variable with the var keyword is that the variable created with the var keyword has a global scope, or a function scope(global means that the variable can be used ANYWHERE in the program, and function scope means that the variable can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[14,15],"class_list":{"0":"post-752","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-javascript-tutorial","7":"tag-javascript","8":"tag-javascript-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=752"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/752\/revisions"}],"predecessor-version":[{"id":1972,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/752\/revisions\/1972"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}