{"id":4185,"date":"2023-05-01T04:16:35","date_gmt":"2023-05-01T04:16:35","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=4185"},"modified":"2023-06-10T11:30:23","modified_gmt":"2023-06-10T11:30:23","slug":"react-es6-tutorial-variables","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/react-es6-tutorial-variables\/","title":{"rendered":"React &#8211; ES6 Tutorial &#8211; Variables"},"content":{"rendered":"<h2><em>What is ES6?<\/em><\/h2>\n<p><em>ES6 stands for ECMAScript 6.<\/em><\/p>\n<p><em>ECMAScript was created to standardize JavaScript, and ES6 is the 6th version of ECMAScript, it was published in 2015, and is also known as ECMAScript 2015.<\/em><\/p>\n<h2>Variables<\/h2>\n<p>Before ES6 there was only one way of defining your variables: with the\u00a0<code class=\"w3-codespan\">var<\/code>\u00a0keyword. If you did not define them, they would be assigned to the global object. Unless you were in strict mode, then you would get an error if your variables were undefined.<\/p>\n<p>Now, with ES6, there are three ways of defining your variables:\u00a0<code class=\"w3-codespan\">var<\/code>,\u00a0<code class=\"w3-codespan\">let<\/code>, and\u00a0<code class=\"w3-codespan\">const<\/code>.<\/p>\n<h3>Example<\/h3>\n<p>var<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">var x = 5.6;<\/pre>\n<p>If you use\u00a0<code class=\"w3-codespan\">var<\/code>\u00a0outside of a function, it belongs to the global scope.<\/p>\n<p>If you use\u00a0<code class=\"w3-codespan\">var<\/code>\u00a0inside of a function, it belongs to that function.<\/p>\n<p>If you use\u00a0<code class=\"w3-codespan\">var<\/code>\u00a0inside of a block, i.e. a for loop, the variable is still available outside of that block.<\/p>\n<div class=\"w3-panel w3-note\">\n<p><span style=\"color: #ff0000;\"><strong><code class=\"w3-codespan\">var<\/code>\u00a0has a\u00a0<em>function<\/em>\u00a0scope, not a\u00a0<em>block<\/em>\u00a0scope.<\/strong><\/span><\/p>\n<\/div>\n<div class=\"w3-example\">\n<h3>Example<\/h3>\n<p>let<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let x = 5.6;<\/pre>\n<p><code class=\"w3-codespan\">let<\/code>\u00a0is the block scoped version of\u00a0<code class=\"w3-codespan\">var<\/code>, and is limited to the block (or expression) where it is defined.<\/p>\n<p>If you use\u00a0<code class=\"w3-codespan\">let<\/code>\u00a0inside of a block, i.e. a for loop, the variable is only available inside of that loop.<\/p>\n<div class=\"w3-panel w3-note\">\n<p><strong><span style=\"color: #ff0000;\"><code class=\"w3-codespan\">let<\/code>\u00a0has a\u00a0<em>block<\/em>\u00a0scope.<\/span><\/strong><\/p>\n<h3>Example<\/h3>\n<p>const<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">const x = 5.6;<\/pre>\n<p><code class=\"w3-codespan\">const<\/code>\u00a0is a variable that once it has been created, its value can never change.<\/p>\n<div class=\"w3-panel w3-note\">\n<p><strong><span style=\"color: #ff0000;\"><code class=\"w3-codespan\">const<\/code>\u00a0has a\u00a0<em>block<\/em>\u00a0scope.<\/span><\/strong><\/p>\n<\/div>\n<p>The keyword\u00a0<code class=\"w3-codespan\">const<\/code>\u00a0is a bit misleading.<\/p>\n<p>It does not define a constant value. It defines a constant reference to a value.<\/p>\n<p>Because of this you can NOT:<\/p>\n<ul>\n<li>Reassign a constant value<\/li>\n<li>Reassign a constant array<\/li>\n<li>Reassign a constant object<\/li>\n<\/ul>\n<p>But you CAN:<\/p>\n<ul>\n<li>Change the elements of constant array<\/li>\n<li>Change the properties of constant object<\/li>\n<\/ul>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>What is ES6? ES6 stands for ECMAScript 6. ECMAScript was created to <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/react-es6-tutorial-variables\/\" title=\"React &#8211; ES6 Tutorial &#8211; Variables\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":4376,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[508,12],"tags":[514,511,510,491,509,513,512,515],"newstopic":[578],"class_list":{"0":"post-4185","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-react","8":"category-web-development","9":"tag-arrow-functions","10":"tag-ecmascript-6","11":"tag-es6-tutorial","12":"tag-javascript","13":"tag-react","14":"tag-react-classes","15":"tag-react-tutorial","16":"tag-variables","17":"newstopic-react"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=4185"}],"version-history":[{"count":1,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4185\/revisions"}],"predecessor-version":[{"id":4187,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4185\/revisions\/4187"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/4376"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=4185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=4185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=4185"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=4185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}