S
S
Sergey Rakipov2020-04-29 04:13:34
JavaScript
Sergey Rakipov, 2020-04-29 04:13:34

Why write const when you can write let?

I sometimes see that they write const (I know the value of this variable), although you can just write let
This is just a feature of the "underline"

It's like the same thing that I see in the layout they write id (I know about uniqueness and priority) when you can just write a class, and I think it's just the way people write.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2020-04-29
@sergei2210

The answer is simple:
const => ReadOnly on a variable.
let => ReadWrite to a variable.
The reasons for protecting a variable from being changed can be absolutely anything.
1. As the most frequent use of protection: multiple use of the value of a constant variable in different blocks of code.
2. Sometimes, they are used as storage, using an object inside a constant.
It is important to remember: the object inside the constant CAN be changed, but the reference to it (the value of the constant itself) cannot be changed.
Well, an excerpt from the docks:

const creates a read-only constant (a new named reference to a memory location). This does not mean that the specified value is immutable, but it does mean that the identifier cannot be reassigned. For example, if a constant points to an object, then the object itself can be changed.

A
Aetae, 2020-04-29
@Aetae

Because eslint forces lol.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question