Overriding the value of const
So I was reading an article and stumbled and saw this CodePen and saw that in increment
method it is setting count
as const
and I'm quite unable to process this, that How can you override const
? Can anyone explain what I'm missing?
state = {
count: 0
};
// Increase count
increment = () => {
const { count } = this.state;
return this.setState({ count: count + 1 });
};
Why declare const
when you can use let
or var
because usage of const
is quite confusing knowing the deconstructed variable is going to be manipulated?
reactjs const
add a comment |
So I was reading an article and stumbled and saw this CodePen and saw that in increment
method it is setting count
as const
and I'm quite unable to process this, that How can you override const
? Can anyone explain what I'm missing?
state = {
count: 0
};
// Increase count
increment = () => {
const { count } = this.state;
return this.setState({ count: count + 1 });
};
Why declare const
when you can use let
or var
because usage of const
is quite confusing knowing the deconstructed variable is going to be manipulated?
reactjs const
1
It's very unclear what you're asking. The code in the question doesn't haveconst
anywhere and it doesn't "override" anything.
– JJJ
Nov 19 '18 at 16:29
@JJJ it seems like someone edited it. and what part are you unclear about the question?
– Yash Karanke
Nov 19 '18 at 16:38
1) Whatconst
are you talking about (there is noconst
in the code) and 2) what part do you think is "overriding" something?
– JJJ
Nov 19 '18 at 16:40
1
@JJJ it should be clear now edited the question and you can also see the codepen
– Yash Karanke
Nov 19 '18 at 16:41
add a comment |
So I was reading an article and stumbled and saw this CodePen and saw that in increment
method it is setting count
as const
and I'm quite unable to process this, that How can you override const
? Can anyone explain what I'm missing?
state = {
count: 0
};
// Increase count
increment = () => {
const { count } = this.state;
return this.setState({ count: count + 1 });
};
Why declare const
when you can use let
or var
because usage of const
is quite confusing knowing the deconstructed variable is going to be manipulated?
reactjs const
So I was reading an article and stumbled and saw this CodePen and saw that in increment
method it is setting count
as const
and I'm quite unable to process this, that How can you override const
? Can anyone explain what I'm missing?
state = {
count: 0
};
// Increase count
increment = () => {
const { count } = this.state;
return this.setState({ count: count + 1 });
};
Why declare const
when you can use let
or var
because usage of const
is quite confusing knowing the deconstructed variable is going to be manipulated?
reactjs const
reactjs const
edited Nov 19 '18 at 16:51
asked Nov 19 '18 at 16:27


Yash Karanke
15918
15918
1
It's very unclear what you're asking. The code in the question doesn't haveconst
anywhere and it doesn't "override" anything.
– JJJ
Nov 19 '18 at 16:29
@JJJ it seems like someone edited it. and what part are you unclear about the question?
– Yash Karanke
Nov 19 '18 at 16:38
1) Whatconst
are you talking about (there is noconst
in the code) and 2) what part do you think is "overriding" something?
– JJJ
Nov 19 '18 at 16:40
1
@JJJ it should be clear now edited the question and you can also see the codepen
– Yash Karanke
Nov 19 '18 at 16:41
add a comment |
1
It's very unclear what you're asking. The code in the question doesn't haveconst
anywhere and it doesn't "override" anything.
– JJJ
Nov 19 '18 at 16:29
@JJJ it seems like someone edited it. and what part are you unclear about the question?
– Yash Karanke
Nov 19 '18 at 16:38
1) Whatconst
are you talking about (there is noconst
in the code) and 2) what part do you think is "overriding" something?
– JJJ
Nov 19 '18 at 16:40
1
@JJJ it should be clear now edited the question and you can also see the codepen
– Yash Karanke
Nov 19 '18 at 16:41
1
1
It's very unclear what you're asking. The code in the question doesn't have
const
anywhere and it doesn't "override" anything.– JJJ
Nov 19 '18 at 16:29
It's very unclear what you're asking. The code in the question doesn't have
const
anywhere and it doesn't "override" anything.– JJJ
Nov 19 '18 at 16:29
@JJJ it seems like someone edited it. and what part are you unclear about the question?
– Yash Karanke
Nov 19 '18 at 16:38
@JJJ it seems like someone edited it. and what part are you unclear about the question?
– Yash Karanke
Nov 19 '18 at 16:38
1) What
const
are you talking about (there is no const
in the code) and 2) what part do you think is "overriding" something?– JJJ
Nov 19 '18 at 16:40
1) What
const
are you talking about (there is no const
in the code) and 2) what part do you think is "overriding" something?– JJJ
Nov 19 '18 at 16:40
1
1
@JJJ it should be clear now edited the question and you can also see the codepen
– Yash Karanke
Nov 19 '18 at 16:41
@JJJ it should be clear now edited the question and you can also see the codepen
– Yash Karanke
Nov 19 '18 at 16:41
add a comment |
4 Answers
4
active
oldest
votes
The code is not overriding the behavior of const
. The increment is happening, not by changing the value of the const count
variable, but by calling setState
which will replace (asynchronously) the entire state of the component. Then the next time increment or decrement is called, count
will receive the new value via the deconstructing assignment from this.state
-- that isn't a change to the variable's value, it is a new variable (local to the increment function) each time increment
is called.
Point taken, but why useconst
, doesn't it a bit of bad practice given that you know you have to change the state?
– Yash Karanke
Nov 19 '18 at 16:48
1
@YashKaranke Yup, horrible example of usingconst
. There are entire camps of people who believe "use const for everything unless the variable has to be reassigned", which leads to ambiguity and also does not tell the reader what is actually important to not be reassigned vs. what just happens to not be reassigned, but won't hurt anything if it is. It dilutes the meaning ofconst
, kinda like the story of the boy who cried wolf. lol
– mhodges
Nov 19 '18 at 16:50
add a comment |
You cannot reassign const
. There's a reason it's named as const
, short form for constant
. It means, once you define it, it cannot be reassigned.
Variable that has to and needs to be reassigned should be declared as let
.
let a = 'a';
a = 'new value'; // this is allowed
const x = 'x';
x = 'new value'; // this is not allowed
var
will also let you reassign a value, but it's an old notation and you'll mostly won't need to use var unless for rare specific cases.
In the code from the question
const { count } = this.state;
the count value is destructured from state and assigned to a variable named count
, meaning it is a copy of the value this.state.count
and not this.state.count
itself.
1
Not sure why someone downvoted this answer 🤔
– Sung Kim
Nov 19 '18 at 16:33
1
I was wondering the same 🧐
– Dinesh Pandiyan
Nov 19 '18 at 16:35
I agree with you, but when you see the given codepen link in the question, it isconst
– Yash Karanke
Nov 19 '18 at 16:39
1
Not the downvoter, but "overridden" is an ambiguous term. The value absolutely can be overridden.const
means the variable cannot be reassigned
– mhodges
Nov 19 '18 at 16:43
2
While this answer is factually correct, I'm not sure that it answers the OP's question. I think they are asking why, if they declarecount
as aconst
(via deconstruction), can that count property on the state object be overwritten. The answer would be that thecount
variable is a copy of the value, not a reference to the property itself. So declaring itconst
is only declaring it for that variable, the object property is untouched.
– mhodges
Nov 19 '18 at 16:47
|
show 3 more comments
When you do const { count } = this.state;
, you are actually creating a copy of this.state.count
, not manipulating this.state.count
directly.
And const
is there as not to accidentally manipulate it unless you need to.
Following code snippet demos that updating "copy" of count
doesn't update the state.count
(click on Run Code Snippet
to see the result).
const state = { count: 0 };
let {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
And this will fail (miserably).
const state = { count: 0 };
const {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
So you can manipulate a copy given if it isconst
?
– Yash Karanke
Nov 19 '18 at 16:54
1
@YashKaranke no you can't, if you had declared it asconst
. I usedlet
to make it modifiable for demo purpose only. If you hadconst {count} = state;
and docount += 999
, the snippet will fail. (updated the answer)
– Sung Kim
Nov 19 '18 at 16:55
add a comment |
You can't override const another meaning is you can't reassign const.
const a = 1;
a = 2; // you will get TypeError. *** numbers and strings are also immutable
but you can modify the constant without reassigning the type
const b = {};
b.a = 'new property'; // { a: "new property" }
const c = ;
c[0] = "new stack"; // ["new stack"]
You can modify but you can't reassign const.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378900%2foverriding-the-value-of-const%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
The code is not overriding the behavior of const
. The increment is happening, not by changing the value of the const count
variable, but by calling setState
which will replace (asynchronously) the entire state of the component. Then the next time increment or decrement is called, count
will receive the new value via the deconstructing assignment from this.state
-- that isn't a change to the variable's value, it is a new variable (local to the increment function) each time increment
is called.
Point taken, but why useconst
, doesn't it a bit of bad practice given that you know you have to change the state?
– Yash Karanke
Nov 19 '18 at 16:48
1
@YashKaranke Yup, horrible example of usingconst
. There are entire camps of people who believe "use const for everything unless the variable has to be reassigned", which leads to ambiguity and also does not tell the reader what is actually important to not be reassigned vs. what just happens to not be reassigned, but won't hurt anything if it is. It dilutes the meaning ofconst
, kinda like the story of the boy who cried wolf. lol
– mhodges
Nov 19 '18 at 16:50
add a comment |
The code is not overriding the behavior of const
. The increment is happening, not by changing the value of the const count
variable, but by calling setState
which will replace (asynchronously) the entire state of the component. Then the next time increment or decrement is called, count
will receive the new value via the deconstructing assignment from this.state
-- that isn't a change to the variable's value, it is a new variable (local to the increment function) each time increment
is called.
Point taken, but why useconst
, doesn't it a bit of bad practice given that you know you have to change the state?
– Yash Karanke
Nov 19 '18 at 16:48
1
@YashKaranke Yup, horrible example of usingconst
. There are entire camps of people who believe "use const for everything unless the variable has to be reassigned", which leads to ambiguity and also does not tell the reader what is actually important to not be reassigned vs. what just happens to not be reassigned, but won't hurt anything if it is. It dilutes the meaning ofconst
, kinda like the story of the boy who cried wolf. lol
– mhodges
Nov 19 '18 at 16:50
add a comment |
The code is not overriding the behavior of const
. The increment is happening, not by changing the value of the const count
variable, but by calling setState
which will replace (asynchronously) the entire state of the component. Then the next time increment or decrement is called, count
will receive the new value via the deconstructing assignment from this.state
-- that isn't a change to the variable's value, it is a new variable (local to the increment function) each time increment
is called.
The code is not overriding the behavior of const
. The increment is happening, not by changing the value of the const count
variable, but by calling setState
which will replace (asynchronously) the entire state of the component. Then the next time increment or decrement is called, count
will receive the new value via the deconstructing assignment from this.state
-- that isn't a change to the variable's value, it is a new variable (local to the increment function) each time increment
is called.
answered Nov 19 '18 at 16:44


Ryan Cogswell
1,560211
1,560211
Point taken, but why useconst
, doesn't it a bit of bad practice given that you know you have to change the state?
– Yash Karanke
Nov 19 '18 at 16:48
1
@YashKaranke Yup, horrible example of usingconst
. There are entire camps of people who believe "use const for everything unless the variable has to be reassigned", which leads to ambiguity and also does not tell the reader what is actually important to not be reassigned vs. what just happens to not be reassigned, but won't hurt anything if it is. It dilutes the meaning ofconst
, kinda like the story of the boy who cried wolf. lol
– mhodges
Nov 19 '18 at 16:50
add a comment |
Point taken, but why useconst
, doesn't it a bit of bad practice given that you know you have to change the state?
– Yash Karanke
Nov 19 '18 at 16:48
1
@YashKaranke Yup, horrible example of usingconst
. There are entire camps of people who believe "use const for everything unless the variable has to be reassigned", which leads to ambiguity and also does not tell the reader what is actually important to not be reassigned vs. what just happens to not be reassigned, but won't hurt anything if it is. It dilutes the meaning ofconst
, kinda like the story of the boy who cried wolf. lol
– mhodges
Nov 19 '18 at 16:50
Point taken, but why use
const
, doesn't it a bit of bad practice given that you know you have to change the state?– Yash Karanke
Nov 19 '18 at 16:48
Point taken, but why use
const
, doesn't it a bit of bad practice given that you know you have to change the state?– Yash Karanke
Nov 19 '18 at 16:48
1
1
@YashKaranke Yup, horrible example of using
const
. There are entire camps of people who believe "use const for everything unless the variable has to be reassigned", which leads to ambiguity and also does not tell the reader what is actually important to not be reassigned vs. what just happens to not be reassigned, but won't hurt anything if it is. It dilutes the meaning of const
, kinda like the story of the boy who cried wolf. lol– mhodges
Nov 19 '18 at 16:50
@YashKaranke Yup, horrible example of using
const
. There are entire camps of people who believe "use const for everything unless the variable has to be reassigned", which leads to ambiguity and also does not tell the reader what is actually important to not be reassigned vs. what just happens to not be reassigned, but won't hurt anything if it is. It dilutes the meaning of const
, kinda like the story of the boy who cried wolf. lol– mhodges
Nov 19 '18 at 16:50
add a comment |
You cannot reassign const
. There's a reason it's named as const
, short form for constant
. It means, once you define it, it cannot be reassigned.
Variable that has to and needs to be reassigned should be declared as let
.
let a = 'a';
a = 'new value'; // this is allowed
const x = 'x';
x = 'new value'; // this is not allowed
var
will also let you reassign a value, but it's an old notation and you'll mostly won't need to use var unless for rare specific cases.
In the code from the question
const { count } = this.state;
the count value is destructured from state and assigned to a variable named count
, meaning it is a copy of the value this.state.count
and not this.state.count
itself.
1
Not sure why someone downvoted this answer 🤔
– Sung Kim
Nov 19 '18 at 16:33
1
I was wondering the same 🧐
– Dinesh Pandiyan
Nov 19 '18 at 16:35
I agree with you, but when you see the given codepen link in the question, it isconst
– Yash Karanke
Nov 19 '18 at 16:39
1
Not the downvoter, but "overridden" is an ambiguous term. The value absolutely can be overridden.const
means the variable cannot be reassigned
– mhodges
Nov 19 '18 at 16:43
2
While this answer is factually correct, I'm not sure that it answers the OP's question. I think they are asking why, if they declarecount
as aconst
(via deconstruction), can that count property on the state object be overwritten. The answer would be that thecount
variable is a copy of the value, not a reference to the property itself. So declaring itconst
is only declaring it for that variable, the object property is untouched.
– mhodges
Nov 19 '18 at 16:47
|
show 3 more comments
You cannot reassign const
. There's a reason it's named as const
, short form for constant
. It means, once you define it, it cannot be reassigned.
Variable that has to and needs to be reassigned should be declared as let
.
let a = 'a';
a = 'new value'; // this is allowed
const x = 'x';
x = 'new value'; // this is not allowed
var
will also let you reassign a value, but it's an old notation and you'll mostly won't need to use var unless for rare specific cases.
In the code from the question
const { count } = this.state;
the count value is destructured from state and assigned to a variable named count
, meaning it is a copy of the value this.state.count
and not this.state.count
itself.
1
Not sure why someone downvoted this answer 🤔
– Sung Kim
Nov 19 '18 at 16:33
1
I was wondering the same 🧐
– Dinesh Pandiyan
Nov 19 '18 at 16:35
I agree with you, but when you see the given codepen link in the question, it isconst
– Yash Karanke
Nov 19 '18 at 16:39
1
Not the downvoter, but "overridden" is an ambiguous term. The value absolutely can be overridden.const
means the variable cannot be reassigned
– mhodges
Nov 19 '18 at 16:43
2
While this answer is factually correct, I'm not sure that it answers the OP's question. I think they are asking why, if they declarecount
as aconst
(via deconstruction), can that count property on the state object be overwritten. The answer would be that thecount
variable is a copy of the value, not a reference to the property itself. So declaring itconst
is only declaring it for that variable, the object property is untouched.
– mhodges
Nov 19 '18 at 16:47
|
show 3 more comments
You cannot reassign const
. There's a reason it's named as const
, short form for constant
. It means, once you define it, it cannot be reassigned.
Variable that has to and needs to be reassigned should be declared as let
.
let a = 'a';
a = 'new value'; // this is allowed
const x = 'x';
x = 'new value'; // this is not allowed
var
will also let you reassign a value, but it's an old notation and you'll mostly won't need to use var unless for rare specific cases.
In the code from the question
const { count } = this.state;
the count value is destructured from state and assigned to a variable named count
, meaning it is a copy of the value this.state.count
and not this.state.count
itself.
You cannot reassign const
. There's a reason it's named as const
, short form for constant
. It means, once you define it, it cannot be reassigned.
Variable that has to and needs to be reassigned should be declared as let
.
let a = 'a';
a = 'new value'; // this is allowed
const x = 'x';
x = 'new value'; // this is not allowed
var
will also let you reassign a value, but it's an old notation and you'll mostly won't need to use var unless for rare specific cases.
In the code from the question
const { count } = this.state;
the count value is destructured from state and assigned to a variable named count
, meaning it is a copy of the value this.state.count
and not this.state.count
itself.
edited Nov 19 '18 at 16:56
answered Nov 19 '18 at 16:31


Dinesh Pandiyan
2,465825
2,465825
1
Not sure why someone downvoted this answer 🤔
– Sung Kim
Nov 19 '18 at 16:33
1
I was wondering the same 🧐
– Dinesh Pandiyan
Nov 19 '18 at 16:35
I agree with you, but when you see the given codepen link in the question, it isconst
– Yash Karanke
Nov 19 '18 at 16:39
1
Not the downvoter, but "overridden" is an ambiguous term. The value absolutely can be overridden.const
means the variable cannot be reassigned
– mhodges
Nov 19 '18 at 16:43
2
While this answer is factually correct, I'm not sure that it answers the OP's question. I think they are asking why, if they declarecount
as aconst
(via deconstruction), can that count property on the state object be overwritten. The answer would be that thecount
variable is a copy of the value, not a reference to the property itself. So declaring itconst
is only declaring it for that variable, the object property is untouched.
– mhodges
Nov 19 '18 at 16:47
|
show 3 more comments
1
Not sure why someone downvoted this answer 🤔
– Sung Kim
Nov 19 '18 at 16:33
1
I was wondering the same 🧐
– Dinesh Pandiyan
Nov 19 '18 at 16:35
I agree with you, but when you see the given codepen link in the question, it isconst
– Yash Karanke
Nov 19 '18 at 16:39
1
Not the downvoter, but "overridden" is an ambiguous term. The value absolutely can be overridden.const
means the variable cannot be reassigned
– mhodges
Nov 19 '18 at 16:43
2
While this answer is factually correct, I'm not sure that it answers the OP's question. I think they are asking why, if they declarecount
as aconst
(via deconstruction), can that count property on the state object be overwritten. The answer would be that thecount
variable is a copy of the value, not a reference to the property itself. So declaring itconst
is only declaring it for that variable, the object property is untouched.
– mhodges
Nov 19 '18 at 16:47
1
1
Not sure why someone downvoted this answer 🤔
– Sung Kim
Nov 19 '18 at 16:33
Not sure why someone downvoted this answer 🤔
– Sung Kim
Nov 19 '18 at 16:33
1
1
I was wondering the same 🧐
– Dinesh Pandiyan
Nov 19 '18 at 16:35
I was wondering the same 🧐
– Dinesh Pandiyan
Nov 19 '18 at 16:35
I agree with you, but when you see the given codepen link in the question, it is
const
– Yash Karanke
Nov 19 '18 at 16:39
I agree with you, but when you see the given codepen link in the question, it is
const
– Yash Karanke
Nov 19 '18 at 16:39
1
1
Not the downvoter, but "overridden" is an ambiguous term. The value absolutely can be overridden.
const
means the variable cannot be reassigned– mhodges
Nov 19 '18 at 16:43
Not the downvoter, but "overridden" is an ambiguous term. The value absolutely can be overridden.
const
means the variable cannot be reassigned– mhodges
Nov 19 '18 at 16:43
2
2
While this answer is factually correct, I'm not sure that it answers the OP's question. I think they are asking why, if they declare
count
as a const
(via deconstruction), can that count property on the state object be overwritten. The answer would be that the count
variable is a copy of the value, not a reference to the property itself. So declaring it const
is only declaring it for that variable, the object property is untouched.– mhodges
Nov 19 '18 at 16:47
While this answer is factually correct, I'm not sure that it answers the OP's question. I think they are asking why, if they declare
count
as a const
(via deconstruction), can that count property on the state object be overwritten. The answer would be that the count
variable is a copy of the value, not a reference to the property itself. So declaring it const
is only declaring it for that variable, the object property is untouched.– mhodges
Nov 19 '18 at 16:47
|
show 3 more comments
When you do const { count } = this.state;
, you are actually creating a copy of this.state.count
, not manipulating this.state.count
directly.
And const
is there as not to accidentally manipulate it unless you need to.
Following code snippet demos that updating "copy" of count
doesn't update the state.count
(click on Run Code Snippet
to see the result).
const state = { count: 0 };
let {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
And this will fail (miserably).
const state = { count: 0 };
const {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
So you can manipulate a copy given if it isconst
?
– Yash Karanke
Nov 19 '18 at 16:54
1
@YashKaranke no you can't, if you had declared it asconst
. I usedlet
to make it modifiable for demo purpose only. If you hadconst {count} = state;
and docount += 999
, the snippet will fail. (updated the answer)
– Sung Kim
Nov 19 '18 at 16:55
add a comment |
When you do const { count } = this.state;
, you are actually creating a copy of this.state.count
, not manipulating this.state.count
directly.
And const
is there as not to accidentally manipulate it unless you need to.
Following code snippet demos that updating "copy" of count
doesn't update the state.count
(click on Run Code Snippet
to see the result).
const state = { count: 0 };
let {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
And this will fail (miserably).
const state = { count: 0 };
const {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
So you can manipulate a copy given if it isconst
?
– Yash Karanke
Nov 19 '18 at 16:54
1
@YashKaranke no you can't, if you had declared it asconst
. I usedlet
to make it modifiable for demo purpose only. If you hadconst {count} = state;
and docount += 999
, the snippet will fail. (updated the answer)
– Sung Kim
Nov 19 '18 at 16:55
add a comment |
When you do const { count } = this.state;
, you are actually creating a copy of this.state.count
, not manipulating this.state.count
directly.
And const
is there as not to accidentally manipulate it unless you need to.
Following code snippet demos that updating "copy" of count
doesn't update the state.count
(click on Run Code Snippet
to see the result).
const state = { count: 0 };
let {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
And this will fail (miserably).
const state = { count: 0 };
const {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
When you do const { count } = this.state;
, you are actually creating a copy of this.state.count
, not manipulating this.state.count
directly.
And const
is there as not to accidentally manipulate it unless you need to.
Following code snippet demos that updating "copy" of count
doesn't update the state.count
(click on Run Code Snippet
to see the result).
const state = { count: 0 };
let {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
And this will fail (miserably).
const state = { count: 0 };
const {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
const state = { count: 0 };
let {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
const state = { count: 0 };
let {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
const state = { count: 0 };
const {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
const state = { count: 0 };
const {count} = state;
count += 999;
console.log(`state.count`, state.count);
console.log(`count`, count);
edited Nov 19 '18 at 16:55
answered Nov 19 '18 at 16:51


Sung Kim
17.4k33111161
17.4k33111161
So you can manipulate a copy given if it isconst
?
– Yash Karanke
Nov 19 '18 at 16:54
1
@YashKaranke no you can't, if you had declared it asconst
. I usedlet
to make it modifiable for demo purpose only. If you hadconst {count} = state;
and docount += 999
, the snippet will fail. (updated the answer)
– Sung Kim
Nov 19 '18 at 16:55
add a comment |
So you can manipulate a copy given if it isconst
?
– Yash Karanke
Nov 19 '18 at 16:54
1
@YashKaranke no you can't, if you had declared it asconst
. I usedlet
to make it modifiable for demo purpose only. If you hadconst {count} = state;
and docount += 999
, the snippet will fail. (updated the answer)
– Sung Kim
Nov 19 '18 at 16:55
So you can manipulate a copy given if it is
const
?– Yash Karanke
Nov 19 '18 at 16:54
So you can manipulate a copy given if it is
const
?– Yash Karanke
Nov 19 '18 at 16:54
1
1
@YashKaranke no you can't, if you had declared it as
const
. I used let
to make it modifiable for demo purpose only. If you had const {count} = state;
and do count += 999
, the snippet will fail. (updated the answer)– Sung Kim
Nov 19 '18 at 16:55
@YashKaranke no you can't, if you had declared it as
const
. I used let
to make it modifiable for demo purpose only. If you had const {count} = state;
and do count += 999
, the snippet will fail. (updated the answer)– Sung Kim
Nov 19 '18 at 16:55
add a comment |
You can't override const another meaning is you can't reassign const.
const a = 1;
a = 2; // you will get TypeError. *** numbers and strings are also immutable
but you can modify the constant without reassigning the type
const b = {};
b.a = 'new property'; // { a: "new property" }
const c = ;
c[0] = "new stack"; // ["new stack"]
You can modify but you can't reassign const.
add a comment |
You can't override const another meaning is you can't reassign const.
const a = 1;
a = 2; // you will get TypeError. *** numbers and strings are also immutable
but you can modify the constant without reassigning the type
const b = {};
b.a = 'new property'; // { a: "new property" }
const c = ;
c[0] = "new stack"; // ["new stack"]
You can modify but you can't reassign const.
add a comment |
You can't override const another meaning is you can't reassign const.
const a = 1;
a = 2; // you will get TypeError. *** numbers and strings are also immutable
but you can modify the constant without reassigning the type
const b = {};
b.a = 'new property'; // { a: "new property" }
const c = ;
c[0] = "new stack"; // ["new stack"]
You can modify but you can't reassign const.
You can't override const another meaning is you can't reassign const.
const a = 1;
a = 2; // you will get TypeError. *** numbers and strings are also immutable
but you can modify the constant without reassigning the type
const b = {};
b.a = 'new property'; // { a: "new property" }
const c = ;
c[0] = "new stack"; // ["new stack"]
You can modify but you can't reassign const.
answered Nov 19 '18 at 16:46


Tan
916
916
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378900%2foverriding-the-value-of-const%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It's very unclear what you're asking. The code in the question doesn't have
const
anywhere and it doesn't "override" anything.– JJJ
Nov 19 '18 at 16:29
@JJJ it seems like someone edited it. and what part are you unclear about the question?
– Yash Karanke
Nov 19 '18 at 16:38
1) What
const
are you talking about (there is noconst
in the code) and 2) what part do you think is "overriding" something?– JJJ
Nov 19 '18 at 16:40
1
@JJJ it should be clear now edited the question and you can also see the codepen
– Yash Karanke
Nov 19 '18 at 16:41