Instructor: Inside our file, we see the magic number variable created with a let binding. Since we're using them let binding, we can use the increment operator. If we run our code, we'll see nine being printed to our console.
If we instead created this binding using const, we'll quickly see that we get a type error when we run our code, and this is because const is used to create an immutable binding. As a result of that, we're not able to mutate our binding with this increment operator.
To illustrate this a little further, I'm again going to use let to create a binding to an array of numbers this time. Then, just going to immediately change this binding, so that it references the array holding 4,5,6. Now, if we run that code, we see that 4,5, and 6 is being printed.
Again, if I change this declaration to utilize const, we'll again get a type error just like we did before. This leads many to believe that using const ensures that the values that it holds are immutable. However, this doesn't happen to be the case.
Although const won't allow us to reassign bindings, it doesn't say anything about reassigning the data that it's bound to. To illustrate this, I'm just going to change the first item inside this array to a new array 4,5,6. Now, if we run this code, we see that instead of printing 1,2,3, it prints 4,5,6,2,3.
Now, this shows that data being held with const is just as susceptible to mutations as that created with the let binding. Show, one way in which we can ensure that our values are also immutable, is to use object.freeze. This is going to create a shallow copy of whatever data is being passed into it.
Once created, we won't be able to add or remove any properties from it, and we won't be able to modify any existing properties as well. If we run our code, we'll see 1,2,3 being printed out, while our mutation has just failed silently.
Now I do want to point out that, in the event that a property inside of our object is an object itself, then that object must also be frozen to enforce immutability. However, since we're just working with the plain array, we're in good hands here.
Now to wrap this up, let's see what happens when we run this code and check code. Now, instead of failing silently, we see that we get a type error saying that we cannot assign a read only property.