{"componentChunkName":"component---src-templates-blog-post-js","path":"/JavaScript/var_let_const/","result":{"data":{"site":{"siteMetadata":{"title":"Progress Not Perfection","author":"Sunmin","siteUrl":"https://sunmin.netlify.com","comment":{"disqusShortName":"","utterances":"Sunmin0520/blog"}}},"markdownRemark":{"id":"8ed8a92a-8ca5-57ea-8f29-0dfe22224486","excerpt":"var로 선언한 변수의 문제점 변수 중복 선언 허용(override) 함수 레벨 스코프(함수의 코드블록만을 지역스코프로 인정) -> 함수 외부에서 선언하면 코드블록내에서 선언해도 전역변수 되어버림 => 전역변수 남발 변수 호이스팅: 변수 선언문이 스코프의 선두로 끌어올려진 것처럼 작동.  var는 선언과 초기화가 한 번에 -> 변수 선언문 이전에 참조 가능(할당은 안되어있으니 undefined) let 변수 중복 선언 금지(syntaxError: Identifier ‘bar’ has already…","html":"<h2 id=\"var로-선언한-변수의-문제점\" style=\"position:relative;\"><a href=\"#var%EB%A1%9C-%EC%84%A0%EC%96%B8%ED%95%9C-%EB%B3%80%EC%88%98%EC%9D%98-%EB%AC%B8%EC%A0%9C%EC%A0%90\" aria-label=\"var로 선언한 변수의 문제점 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>var로 선언한 변수의 문제점</h2>\n<ol>\n<li>변수 중복 선언 허용(override)</li>\n<li><strong>함수 레벨 스코프(함수의 코드블록만을 지역스코프로 인정)</strong> -> 함수 외부에서 선언하면 코드블록내에서 선언해도 전역변수 되어버림 => 전역변수 남발</li>\n<li>변수 호이스팅: 변수 선언문이 스코프의 선두로 끌어올려진 것처럼 작동. </li>\n<li>var는 선언과 초기화가 한 번에 -> 변수 선언문 이전에 참조 가능(할당은 안되어있으니 undefined)</li>\n</ol>\n<h2 id=\"let\" style=\"position:relative;\"><a href=\"#let\" aria-label=\"let permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>let</h2>\n<ol>\n<li>변수 중복 선언 금지<font size = 2>(syntaxError: Identifier ‘bar’ has already been declared)</font></li>\n<li>블록 레벨 스코프</li>\n<li>\n<p>변수 호이스팅: 변수 호이스팅 작동하지 않는 것<strong>처럼</strong> 작동 </p>\n<ul>\n<li>\n<p>let은 선언과 초기화가 따로 -> 변수 선언문 이전에 참조시 ReferenceError</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">let</span> foo <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span><span class=\"token comment\">//전역변수</span>\n<span class=\"token operator\">...</span>\n<span class=\"token punctuation\">{</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>foo<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span><span class=\"token comment\">//ReferenceError</span>\n<span class=\"token keyword\">let</span> foo <span class=\"token operator\">=</span> <span class=\"token number\">2</span><span class=\"token punctuation\">;</span><span class=\"token comment\">//지역변수</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n</li>\n<li>만약 여기서 호이스팅이 발생하지 않았다면 console.log(foo)에서 foo는 전역변수 let foo = 1을 참조했어야한다.</li>\n<li><strong>JS는 모든 선언(var,let,const,function, function*, class)에서 호이스팅 발생 but let,const는 호이스팅 발생 안 하는 것처럼 작동</strong></li>\n</ul>\n</li>\n<li>let으로 선언한 전역변수는 window의 프로퍼티가 아님</li>\n</ol>\n<h2 id=\"const\" style=\"position:relative;\"><a href=\"#const\" aria-label=\"const permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>const</h2>\n<ol>\n<li>선언과 할당 동시에 해야함</li>\n<li>\n<p>재할당 금지</p>\n<ul>\n<li>const로 객체를 할당한 경우 프로퍼티의 변경 통해서 값 변경 가능 => const는 재할당을 금지할 뿐 불변은 아님!</li>\n</ul>\n</li>\n<li>let과 공통점: 블록 레벨 스코프, 호이스팅하지 않는 것처럼 작동</li>\n</ol>\n<p>\n<br />\n<br />\n</p>\n<p><strong>출처</strong> 이웅모, 모던 자바스크립트 Deep Dive(위키북스, 2020)</p>","frontmatter":{"title":"[JS] var, let, const","date":"February 07, 2021"}}},"pageContext":{"slug":"/JavaScript/var_let_const/","previous":{"fields":{"slug":"/Retrospective/2021/Feb_1st/"},"frontmatter":{"title":"[회고] 2월 첫째 주 회고(2/1 ~ 2/7)","category":"retrospective","draft":false}},"next":{"fields":{"slug":"/Web/https/"},"frontmatter":{"title":"[Web] HTTPS, SSL","category":"web","draft":false}}}},"staticQueryHashes":["3128451518","521680639"]}