Coding Newbie

javascript const declaration

Introducing CONST: New JavaScript Declaration

ECMAScript 2015, also known as ES6, is a new JavaScript standard that introduced two new declarations: LET and CONST. If you wish to learn about LET variable first, read it in a post “VAR And LET: All You Must Know About JavaScript Variables“.

Both declarations, LET and CONST, have the same scope behavior, block scope. Block scope means that the variable only exists only inside the scope where defined (inside the function, object, or as a global variable). Simply said, the variable exists only between the curly braces where we have declared it.

But what is the difference between LET and CONST?

What is a CONST?

Unlike LET variables, where we can change their value, CONST has constant value that can not be changed and reassigned. If you try to change the value of a CONST, you will get the error message.

javascript const

On the other hand, if we use LET, we can make the above changes of “x”.

Why do we use CONST?

Actually, many people tend to use LET instead of CONST when writing their code. You can again use LET, and not change its value, so it behaves just like CONST.

However, the code is more efficient when we use CONST declaration when it comes to the way it runs and uses the machine memory. If you have a variable in your code, but you know that its value is never going to change, using CONST will not leave additional space in computer’s memory for later changes. This is the case if you use LET.

If you do not need value reassignment, CONST is a better choice than LET.

In addition, using CONST prevents you from making the mistakes of assigning new value to an already declared CONST, because the code itself would not let you do it.

Due to its properties of memory optimization and reassigning prevention, CONST found its use as a part of function declaration. It is a neat way to store the functions:

javascript const function

Can we add new value to CONST?

The fact that CONST prevents assigning new value, that does not mean you can not add values to declared CONST in your code.

In order for this to be possible, you have to create a CONST like an array, or an object. However, you still can not just assign new values. In that case, you have to use methods.

Let’s illustrate that with an example in order to better understand it.

javascript const add value

Conclusion

Using CONST makes the code easier to read. If you have a lot of code written, having constant values will definitely come handy and help you track better your own code.

It also prevents you changing it by mistake, which makes CONST option less error prone.

In the end, the computer uses far less memory for CONST, and that can reduce compiling time.

Both VAR and LET represent variable declarations, but LET is a newer variable type. Using CONST and LET is a new standard of writing JavaScript code. Learn about LET in the post post “VAR And LET: All You Must Know About JavaScript Variables“.

%d bloggers like this: