{"id":13435,"date":"2022-02-09T20:02:49","date_gmt":"2022-02-09T20:02:49","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=13435"},"modified":"2024-02-16T10:47:41","modified_gmt":"2024-02-16T10:47:41","slug":"var-vs-let-vs-const-js","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=13435","title":{"rendered":"var vs. let vs. const (JS)"},"content":{"rendered":"<p><strong>Overview<\/strong><\/p>\n<table>\n<tr>\n<th>Type<\/th>\n<th>Scope<\/th>\n<th>Reassignable<\/th>\n<th>Redeclarable<\/th>\n<\/tr>\n<tr>\n<th>var<\/th>\n<td>function<\/td>\n<td>yes<\/td>\n<td>yes<\/td>\n<\/tr>\n<tr>\n<th>let<\/th>\n<td>block<\/td>\n<td>yes<\/td>\n<td>no<\/td>\n<\/tr>\n<tr>\n<th>const<\/th>\n<td>block<\/td>\n<td>no<\/td>\n<td>no<\/td>\n<\/tr>\n<\/table>\n<hr>\n<p><strong>var<\/strong><\/p>\n<pre class=\"brush: jscript; gutter: false; title: ; notranslate\" title=\"\">\r\nfunction fct()\r\n{\r\n  var v1 = 1;\r\n  {\r\n    var v2 = 2;\r\n  }\r\n  console.log(v1); \/\/ ok\r\n  v1 = 3;          \/\/ ok\r\n  console.log(v2); \/\/ ok\r\n  var v1 = 4;      \/\/ ok\r\n}\r\nconsole.log(v1);   \/\/ error\r\n<\/pre>\n<hr>\n<p><strong>let<\/strong><\/p>\n<pre class=\"brush: jscript; gutter: false; title: ; notranslate\" title=\"\">\r\nfunction fct()\r\n{\r\n  let v1 = 1;\r\n  {\r\n    let v2 = 2;\r\n  }\r\n  console.log(v1); \/\/ ok\r\n  v1 = 3;          \/\/ ok\r\n  console.log(v2); \/\/ error\r\n  let v1 = 4;      \/\/ error\r\n}\r\nconsole.log(v1);   \/\/ error\r\n<\/pre>\n<hr>\n<p><strong>const<\/strong><\/p>\n<pre class=\"brush: jscript; gutter: false; title: ; notranslate\" title=\"\">\r\nfunction fct()\r\n{\r\n  const v1 = 1;\r\n  {\r\n    const v2 = 2;\r\n  }\r\n  console.log(v1); \/\/ ok\r\n  v1 = 3;          \/\/ error\r\n  console.log(v2); \/\/ error\r\n  const v1 = 4;    \/\/ error\r\n}\r\nconsole.log(v1);   \/\/ error\r\n<\/pre>\n<hr>\n<p><strong>Application<\/strong><\/p>\n<ul>\n<li>Depending on the circumstances try to use &#8216;const&#8217; over &#8216;let&#8217; over &#8216;var&#8217;.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Overview Type Scope Reassignable Redeclarable var function yes yes let block yes no const block no no var let const<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[55],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/13435"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13435"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/13435\/revisions"}],"predecessor-version":[{"id":16833,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/13435\/revisions\/16833"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13435"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}