× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

ES2015 let vs const

Jan 26, 2016
<p>So like everyhone knows or should know, the future of javascript is here, and everyones should give a try.</p> <p>Thats why im going to try to put some tips or ways to do stuff over here, like the title say, we are going to show an example of let and const and how developers are using it.</p> <p> </p> <p>So whats its <strong>let</strong>?</p> <p>let is the new way of decalring variables and they scoope, so</p> <pre><code class="language-javascript">if(x === true){ var isTrue = true; }; console.log(isTrue) // this will print true //ES2015 Way if(x === true){ let isTrue = true; }; console.log(isTrue) // error, isTrue its just scoope avaible inside the if block</code></pre> <p>Isn't that great??, this will make the code more readable and clean, something you should be looking always.</p> <p>Now <strong>const, </strong>the way you declare const in right now should be something like.</p> <pre><code class="language-javascript">//Uppercase way. MAX_NUMBER = 10; </code></pre> <p>So this dosnt really tells javascriot MAX_NUMBER its a const, its just a way to make you know MAX_NUMBER is a const, so lather on your code you can do something like</p> <pre><code>MAX_NUMBER = 11 // This will work</code></pre> <p>and thats somethign we dont expect from a const right?</p> <p>So ES2015 to the attack.</p> <p>with ES2015, you can actually tell JS what is a const and what no using the const keyword</p> <pre><code class="language-javascript">const MAX_NUMBER = 10 MAX_NUMBER = 11 // will throw an error</code></pre> <p> </p> <p>So back to the tittle discussion,  what you should use let or const?, and mostly developer like to use const right now, because the next scneario for example.</p> <pre><code>const person = { sex : 'man' }; person.sex = 'woman' // this will work console.log(person.sex) // will print 'woman'. </code></pre> <p> </p> <p>But but.. why this work? dont you say that const dosnt let you change the variable?</p> <p>the reason its because we are not changing the variable itself, we are changing the field inside the variable in this case 'sex' </p> <p> </p> <p>Saying that, i will try to keep up some stuff about ES2015</p>
comments powered by Disqus