Why doesn't my equality comparison using = (a single equals) work correctly? [duplicate]
This question already has an answer here:
Why doesn't my simple if-statement render false in javascript?
2 answers
I'm trying to check if a string is blank, less than or equal to 9 digits, or up to 10 digits. But it always follows the else if (str.length <= 9)
.
if (str = ''){
console.log("The string cannot be blank");
} else if (str.length <= 9) {
console.log("The string must be at least 9 characters long");
} else if (str.length <= 10) {
console.log("The string is long enough.");
}
No matter what I put in, I always get The string must be at least 9 characters long
. Why?
javascript equality
marked as duplicate by Oriol
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 15 '16 at 18:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 2 more comments
This question already has an answer here:
Why doesn't my simple if-statement render false in javascript?
2 answers
I'm trying to check if a string is blank, less than or equal to 9 digits, or up to 10 digits. But it always follows the else if (str.length <= 9)
.
if (str = ''){
console.log("The string cannot be blank");
} else if (str.length <= 9) {
console.log("The string must be at least 9 characters long");
} else if (str.length <= 10) {
console.log("The string is long enough.");
}
No matter what I put in, I always get The string must be at least 9 characters long
. Why?
javascript equality
marked as duplicate by Oriol
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 15 '16 at 18:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
I'm sure there must be a canonical question for this...
– T.J. Crowder
Jul 8 '16 at 13:50
1
@T.J.Crowder — Possibly, but I'd be surprised if any of them had a title as clear as this one. Mostly they come to do "My code doesn't work and I don't know why"
– Quentin
Jul 8 '16 at 13:54
@Quentin: Yeah. I'm editing this into one. :-)
– T.J. Crowder
Jul 8 '16 at 13:54
@T.J.Crowder — Ooops, didn't notice that was your edit :)
– Quentin
Jul 8 '16 at 13:55
@Quentin: No worries, that's a better title
– T.J. Crowder
Jul 8 '16 at 13:57
|
show 2 more comments
This question already has an answer here:
Why doesn't my simple if-statement render false in javascript?
2 answers
I'm trying to check if a string is blank, less than or equal to 9 digits, or up to 10 digits. But it always follows the else if (str.length <= 9)
.
if (str = ''){
console.log("The string cannot be blank");
} else if (str.length <= 9) {
console.log("The string must be at least 9 characters long");
} else if (str.length <= 10) {
console.log("The string is long enough.");
}
No matter what I put in, I always get The string must be at least 9 characters long
. Why?
javascript equality
This question already has an answer here:
Why doesn't my simple if-statement render false in javascript?
2 answers
I'm trying to check if a string is blank, less than or equal to 9 digits, or up to 10 digits. But it always follows the else if (str.length <= 9)
.
if (str = ''){
console.log("The string cannot be blank");
} else if (str.length <= 9) {
console.log("The string must be at least 9 characters long");
} else if (str.length <= 10) {
console.log("The string is long enough.");
}
No matter what I put in, I always get The string must be at least 9 characters long
. Why?
This question already has an answer here:
Why doesn't my simple if-statement render false in javascript?
2 answers
javascript equality
javascript equality
edited Jul 8 '16 at 13:57
T.J. Crowder
681k12112061302
681k12112061302
asked Jul 8 '16 at 13:48
SamSam
195
195
marked as duplicate by Oriol
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 15 '16 at 18:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Oriol
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 15 '16 at 18:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
I'm sure there must be a canonical question for this...
– T.J. Crowder
Jul 8 '16 at 13:50
1
@T.J.Crowder — Possibly, but I'd be surprised if any of them had a title as clear as this one. Mostly they come to do "My code doesn't work and I don't know why"
– Quentin
Jul 8 '16 at 13:54
@Quentin: Yeah. I'm editing this into one. :-)
– T.J. Crowder
Jul 8 '16 at 13:54
@T.J.Crowder — Ooops, didn't notice that was your edit :)
– Quentin
Jul 8 '16 at 13:55
@Quentin: No worries, that's a better title
– T.J. Crowder
Jul 8 '16 at 13:57
|
show 2 more comments
2
I'm sure there must be a canonical question for this...
– T.J. Crowder
Jul 8 '16 at 13:50
1
@T.J.Crowder — Possibly, but I'd be surprised if any of them had a title as clear as this one. Mostly they come to do "My code doesn't work and I don't know why"
– Quentin
Jul 8 '16 at 13:54
@Quentin: Yeah. I'm editing this into one. :-)
– T.J. Crowder
Jul 8 '16 at 13:54
@T.J.Crowder — Ooops, didn't notice that was your edit :)
– Quentin
Jul 8 '16 at 13:55
@Quentin: No worries, that's a better title
– T.J. Crowder
Jul 8 '16 at 13:57
2
2
I'm sure there must be a canonical question for this...
– T.J. Crowder
Jul 8 '16 at 13:50
I'm sure there must be a canonical question for this...
– T.J. Crowder
Jul 8 '16 at 13:50
1
1
@T.J.Crowder — Possibly, but I'd be surprised if any of them had a title as clear as this one. Mostly they come to do "My code doesn't work and I don't know why"
– Quentin
Jul 8 '16 at 13:54
@T.J.Crowder — Possibly, but I'd be surprised if any of them had a title as clear as this one. Mostly they come to do "My code doesn't work and I don't know why"
– Quentin
Jul 8 '16 at 13:54
@Quentin: Yeah. I'm editing this into one. :-)
– T.J. Crowder
Jul 8 '16 at 13:54
@Quentin: Yeah. I'm editing this into one. :-)
– T.J. Crowder
Jul 8 '16 at 13:54
@T.J.Crowder — Ooops, didn't notice that was your edit :)
– Quentin
Jul 8 '16 at 13:55
@T.J.Crowder — Ooops, didn't notice that was your edit :)
– Quentin
Jul 8 '16 at 13:55
@Quentin: No worries, that's a better title
– T.J. Crowder
Jul 8 '16 at 13:57
@Quentin: No worries, that's a better title
– T.J. Crowder
Jul 8 '16 at 13:57
|
show 2 more comments
1 Answer
1
active
oldest
votes
=
is always assignment. Equality comparison is ==
(loose, coerces types to try to make a match) or ===
(no type coercion).
So you want
if (str === ''){
// -----^^^
not
// NOT THIS
if (str = ''){
// -----^
What happens when you do if (str = '')
is that the assignment str = ''
is done, and then the resulting value (''
) is tested, effectively like this (if we ignore a couple of details):
str = '';
if (str) {
Since ''
is a falsy value in JavaScript, that check will be false and it goes to the else if (str.length <= 9)
step. Since at that point, str.length
is 0
, that's the path the code takes.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
=
is always assignment. Equality comparison is ==
(loose, coerces types to try to make a match) or ===
(no type coercion).
So you want
if (str === ''){
// -----^^^
not
// NOT THIS
if (str = ''){
// -----^
What happens when you do if (str = '')
is that the assignment str = ''
is done, and then the resulting value (''
) is tested, effectively like this (if we ignore a couple of details):
str = '';
if (str) {
Since ''
is a falsy value in JavaScript, that check will be false and it goes to the else if (str.length <= 9)
step. Since at that point, str.length
is 0
, that's the path the code takes.
add a comment |
=
is always assignment. Equality comparison is ==
(loose, coerces types to try to make a match) or ===
(no type coercion).
So you want
if (str === ''){
// -----^^^
not
// NOT THIS
if (str = ''){
// -----^
What happens when you do if (str = '')
is that the assignment str = ''
is done, and then the resulting value (''
) is tested, effectively like this (if we ignore a couple of details):
str = '';
if (str) {
Since ''
is a falsy value in JavaScript, that check will be false and it goes to the else if (str.length <= 9)
step. Since at that point, str.length
is 0
, that's the path the code takes.
add a comment |
=
is always assignment. Equality comparison is ==
(loose, coerces types to try to make a match) or ===
(no type coercion).
So you want
if (str === ''){
// -----^^^
not
// NOT THIS
if (str = ''){
// -----^
What happens when you do if (str = '')
is that the assignment str = ''
is done, and then the resulting value (''
) is tested, effectively like this (if we ignore a couple of details):
str = '';
if (str) {
Since ''
is a falsy value in JavaScript, that check will be false and it goes to the else if (str.length <= 9)
step. Since at that point, str.length
is 0
, that's the path the code takes.
=
is always assignment. Equality comparison is ==
(loose, coerces types to try to make a match) or ===
(no type coercion).
So you want
if (str === ''){
// -----^^^
not
// NOT THIS
if (str = ''){
// -----^
What happens when you do if (str = '')
is that the assignment str = ''
is done, and then the resulting value (''
) is tested, effectively like this (if we ignore a couple of details):
str = '';
if (str) {
Since ''
is a falsy value in JavaScript, that check will be false and it goes to the else if (str.length <= 9)
step. Since at that point, str.length
is 0
, that's the path the code takes.
edited Jul 8 '16 at 14:00
community wiki
3 revs
T.J. Crowder
add a comment |
add a comment |
2
I'm sure there must be a canonical question for this...
– T.J. Crowder
Jul 8 '16 at 13:50
1
@T.J.Crowder — Possibly, but I'd be surprised if any of them had a title as clear as this one. Mostly they come to do "My code doesn't work and I don't know why"
– Quentin
Jul 8 '16 at 13:54
@Quentin: Yeah. I'm editing this into one. :-)
– T.J. Crowder
Jul 8 '16 at 13:54
@T.J.Crowder — Ooops, didn't notice that was your edit :)
– Quentin
Jul 8 '16 at 13:55
@Quentin: No worries, that's a better title
– T.J. Crowder
Jul 8 '16 at 13:57