What are const and let
- Const is the keyword used to declare a variable where we expect that the value will never change.
- Let is the keyword used to declare a variable where we expect that the value will change at some point in the future.
So let’s have a look at an example. In the ES5 code below we have a few variables describing a person where we would use var to declare all the variables.
// ES5 var name = 'Billy'; var dob = '01/01/1990'; var age = 30; var occupation = 'Web Developer';
For our first variable, name, we’d expect that it is extremely unlikely that someone’s name will change. Since we expect the value for name will never change, we would use const instead of var.
For the dob variable, the person’s date of birth will never change so we’ll use const again.
For age, we can see that Billy is currently 30 years old and we know that next year Billy will be 31. Since we expect the age variable to change we’ll use let.
Billy’s current occupation is a Web Developer. If Billy stays at his current place of employment we’d hope Billy will eventually be promoted to a Senior Web Developer. So since we expect Billy’s occupation to be updated at some point in the future, we’ll use let.
// ES6 const name = 'Billy'; // Billy will always be called Billy const dob = '01/01/1990'; // Billy will always be born January 1st, 1990 let age = 30; // Billy will increase in age every year let occupation = 'Web Developer'; // Billy will be promoted to Senior Web Developer // A few years later… // We don’t need to use ‘let’ again once the variable has been declared age = 34; occupation = 'Senior Web Developer';
From the code above, we can see that let acts very similar to var as it can be changed over time. However, once a variable has been declared with const we’ll be thrown an error is we try to update that variable.
Why bother using const/let
The use of const and let is a quality of life improvement to your codebase.
It will let future you and other developers to be able to quickly glance at variables and instantly be able to know if those variables will be updated later in the code or will always be the same.
Using robots.txt to prevent staging sites from indexing
Going over the basics of the robots.txt and the solution to prevent your staging site from being indexed.
CodePen Challenge: Image Hovers
This week's challenge prompt was to design a hover effect for an image. I created a simple effect that will jiggle the jelly when it is moused over!
WordPress: The Gutenberg Controversy
Gutenberg aims to replace the old CMS text editor WordPress user’s have come to know and love with these new movable blocks. Let’s take a look at what went wrong and if there’s any hope for Gutenberg’s future.
forEach() is an ES6 helper that is used to call a function once on each item in an array and is arguably the most useful ES6 helper. Let's dig into how it works.