Coercion in JavaScript
I was wondering a few things about coercion.
When you do:
1 == true // true
Which one is coerced into which one ? is it the left one or the right one ?
When you do
undefined == null // true
How does it work exactly ?
In which order does it try to convert it ?
By instance:
1) String(undefined) == String(null) // false
2) Number(undefined) == Number(null) // false
3) Boolean(undefined) == Boolean(null) // true
Does it first try to coerce the left side operand ? then the right ? then both ?
EDIT:
As explained in the comments:
"not a duplicate. While both questions are about type coercion, this one asks which operand get coerced into the other. The other one is about the source of truth when evaluating the coerced types"
javascript coercion
|
show 4 more comments
I was wondering a few things about coercion.
When you do:
1 == true // true
Which one is coerced into which one ? is it the left one or the right one ?
When you do
undefined == null // true
How does it work exactly ?
In which order does it try to convert it ?
By instance:
1) String(undefined) == String(null) // false
2) Number(undefined) == Number(null) // false
3) Boolean(undefined) == Boolean(null) // true
Does it first try to coerce the left side operand ? then the right ? then both ?
EDIT:
As explained in the comments:
"not a duplicate. While both questions are about type coercion, this one asks which operand get coerced into the other. The other one is about the source of truth when evaluating the coerced types"
javascript coercion
4
developer.mozilla.org/en-US/docs/Web/JavaScript/…
– VLAZ
Feb 7 at 6:33
5
@adiga definitely not a duplicate. While both questions are about type coercion, this one asks which operand get coerced into the other. The other one is about the source of truth when evaluating the coerced types
– molamk
Feb 7 at 6:41
2
@adiga Its not a dupe. Marked link is checking equality and this post is asking the process of equality. Its like Why 1 == true is true vs How 1 == true is true
– Rajesh
Feb 7 at 6:41
2
@Rajesh It's a possible duplicate. They are related. It's useful future users (and OP) who come to this question and might want to read the linked question.
– adiga
Feb 7 at 6:45
2
@adiga: Duplicates are "duplicate questions", not "related questions with similar answers". So it's definitely not a duplicate.
– Eric Duminil
Feb 7 at 8:41
|
show 4 more comments
I was wondering a few things about coercion.
When you do:
1 == true // true
Which one is coerced into which one ? is it the left one or the right one ?
When you do
undefined == null // true
How does it work exactly ?
In which order does it try to convert it ?
By instance:
1) String(undefined) == String(null) // false
2) Number(undefined) == Number(null) // false
3) Boolean(undefined) == Boolean(null) // true
Does it first try to coerce the left side operand ? then the right ? then both ?
EDIT:
As explained in the comments:
"not a duplicate. While both questions are about type coercion, this one asks which operand get coerced into the other. The other one is about the source of truth when evaluating the coerced types"
javascript coercion
I was wondering a few things about coercion.
When you do:
1 == true // true
Which one is coerced into which one ? is it the left one or the right one ?
When you do
undefined == null // true
How does it work exactly ?
In which order does it try to convert it ?
By instance:
1) String(undefined) == String(null) // false
2) Number(undefined) == Number(null) // false
3) Boolean(undefined) == Boolean(null) // true
Does it first try to coerce the left side operand ? then the right ? then both ?
EDIT:
As explained in the comments:
"not a duplicate. While both questions are about type coercion, this one asks which operand get coerced into the other. The other one is about the source of truth when evaluating the coerced types"
javascript coercion
javascript coercion
edited Feb 7 at 9:18
Scipion
asked Feb 7 at 6:30
ScipionScipion
2,99953886
2,99953886
4
developer.mozilla.org/en-US/docs/Web/JavaScript/…
– VLAZ
Feb 7 at 6:33
5
@adiga definitely not a duplicate. While both questions are about type coercion, this one asks which operand get coerced into the other. The other one is about the source of truth when evaluating the coerced types
– molamk
Feb 7 at 6:41
2
@adiga Its not a dupe. Marked link is checking equality and this post is asking the process of equality. Its like Why 1 == true is true vs How 1 == true is true
– Rajesh
Feb 7 at 6:41
2
@Rajesh It's a possible duplicate. They are related. It's useful future users (and OP) who come to this question and might want to read the linked question.
– adiga
Feb 7 at 6:45
2
@adiga: Duplicates are "duplicate questions", not "related questions with similar answers". So it's definitely not a duplicate.
– Eric Duminil
Feb 7 at 8:41
|
show 4 more comments
4
developer.mozilla.org/en-US/docs/Web/JavaScript/…
– VLAZ
Feb 7 at 6:33
5
@adiga definitely not a duplicate. While both questions are about type coercion, this one asks which operand get coerced into the other. The other one is about the source of truth when evaluating the coerced types
– molamk
Feb 7 at 6:41
2
@adiga Its not a dupe. Marked link is checking equality and this post is asking the process of equality. Its like Why 1 == true is true vs How 1 == true is true
– Rajesh
Feb 7 at 6:41
2
@Rajesh It's a possible duplicate. They are related. It's useful future users (and OP) who come to this question and might want to read the linked question.
– adiga
Feb 7 at 6:45
2
@adiga: Duplicates are "duplicate questions", not "related questions with similar answers". So it's definitely not a duplicate.
– Eric Duminil
Feb 7 at 8:41
4
4
developer.mozilla.org/en-US/docs/Web/JavaScript/…
– VLAZ
Feb 7 at 6:33
developer.mozilla.org/en-US/docs/Web/JavaScript/…
– VLAZ
Feb 7 at 6:33
5
5
@adiga definitely not a duplicate. While both questions are about type coercion, this one asks which operand get coerced into the other. The other one is about the source of truth when evaluating the coerced types
– molamk
Feb 7 at 6:41
@adiga definitely not a duplicate. While both questions are about type coercion, this one asks which operand get coerced into the other. The other one is about the source of truth when evaluating the coerced types
– molamk
Feb 7 at 6:41
2
2
@adiga Its not a dupe. Marked link is checking equality and this post is asking the process of equality. Its like Why 1 == true is true vs How 1 == true is true
– Rajesh
Feb 7 at 6:41
@adiga Its not a dupe. Marked link is checking equality and this post is asking the process of equality. Its like Why 1 == true is true vs How 1 == true is true
– Rajesh
Feb 7 at 6:41
2
2
@Rajesh It's a possible duplicate. They are related. It's useful future users (and OP) who come to this question and might want to read the linked question.
– adiga
Feb 7 at 6:45
@Rajesh It's a possible duplicate. They are related. It's useful future users (and OP) who come to this question and might want to read the linked question.
– adiga
Feb 7 at 6:45
2
2
@adiga: Duplicates are "duplicate questions", not "related questions with similar answers". So it's definitely not a duplicate.
– Eric Duminil
Feb 7 at 8:41
@adiga: Duplicates are "duplicate questions", not "related questions with similar answers". So it's definitely not a duplicate.
– Eric Duminil
Feb 7 at 8:41
|
show 4 more comments
1 Answer
1
active
oldest
votes
The process is described at 7.2.12 Abstract Equality Comparison:
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
If Type(x) is the same as Type(y), then return the result of performing Strict Equality Comparison x === y.
If x is null and y is undefined, return true.
If x is undefined and y is null, return true.
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).
If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.
Return false.
So rather than coercing one side and then the other, or something like that, it's more that the interpreter goes through that list above until it finds a matching condition, and executes the resulting command, which may involve coercing only the left side, or only the right side (and, rarely, both, in case a recursive command is reached, such as with true == '1'
, which will fulfill condition 8, turn into 1 == '1'
, fulfilling condition 6 and turning into 1 == 1
, fulfilling condition 3 and resolving to true
)
1
@SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?
– Kaiido
Feb 7 at 6:42
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
Feb 7 at 6:47
@SalmanA not yet, but in the next occurence ofthe comparison ToNumber(x) == y.
it may be no?.
– Kaiido
Feb 7 at 6:47
2
@SalmanA 8 calls the==
process recursively, so I'm pretty sure it's possible? Considertrue == '1'
– CertainPerformance
Feb 7 at 6:48
@CertainPerformance yes, your edit makes it clear.
– Salman A
Feb 7 at 6:51
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%2f54567524%2fcoercion-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The process is described at 7.2.12 Abstract Equality Comparison:
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
If Type(x) is the same as Type(y), then return the result of performing Strict Equality Comparison x === y.
If x is null and y is undefined, return true.
If x is undefined and y is null, return true.
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).
If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.
Return false.
So rather than coercing one side and then the other, or something like that, it's more that the interpreter goes through that list above until it finds a matching condition, and executes the resulting command, which may involve coercing only the left side, or only the right side (and, rarely, both, in case a recursive command is reached, such as with true == '1'
, which will fulfill condition 8, turn into 1 == '1'
, fulfilling condition 6 and turning into 1 == 1
, fulfilling condition 3 and resolving to true
)
1
@SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?
– Kaiido
Feb 7 at 6:42
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
Feb 7 at 6:47
@SalmanA not yet, but in the next occurence ofthe comparison ToNumber(x) == y.
it may be no?.
– Kaiido
Feb 7 at 6:47
2
@SalmanA 8 calls the==
process recursively, so I'm pretty sure it's possible? Considertrue == '1'
– CertainPerformance
Feb 7 at 6:48
@CertainPerformance yes, your edit makes it clear.
– Salman A
Feb 7 at 6:51
add a comment |
The process is described at 7.2.12 Abstract Equality Comparison:
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
If Type(x) is the same as Type(y), then return the result of performing Strict Equality Comparison x === y.
If x is null and y is undefined, return true.
If x is undefined and y is null, return true.
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).
If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.
Return false.
So rather than coercing one side and then the other, or something like that, it's more that the interpreter goes through that list above until it finds a matching condition, and executes the resulting command, which may involve coercing only the left side, or only the right side (and, rarely, both, in case a recursive command is reached, such as with true == '1'
, which will fulfill condition 8, turn into 1 == '1'
, fulfilling condition 6 and turning into 1 == 1
, fulfilling condition 3 and resolving to true
)
1
@SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?
– Kaiido
Feb 7 at 6:42
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
Feb 7 at 6:47
@SalmanA not yet, but in the next occurence ofthe comparison ToNumber(x) == y.
it may be no?.
– Kaiido
Feb 7 at 6:47
2
@SalmanA 8 calls the==
process recursively, so I'm pretty sure it's possible? Considertrue == '1'
– CertainPerformance
Feb 7 at 6:48
@CertainPerformance yes, your edit makes it clear.
– Salman A
Feb 7 at 6:51
add a comment |
The process is described at 7.2.12 Abstract Equality Comparison:
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
If Type(x) is the same as Type(y), then return the result of performing Strict Equality Comparison x === y.
If x is null and y is undefined, return true.
If x is undefined and y is null, return true.
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).
If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.
Return false.
So rather than coercing one side and then the other, or something like that, it's more that the interpreter goes through that list above until it finds a matching condition, and executes the resulting command, which may involve coercing only the left side, or only the right side (and, rarely, both, in case a recursive command is reached, such as with true == '1'
, which will fulfill condition 8, turn into 1 == '1'
, fulfilling condition 6 and turning into 1 == 1
, fulfilling condition 3 and resolving to true
)
The process is described at 7.2.12 Abstract Equality Comparison:
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
If Type(x) is the same as Type(y), then return the result of performing Strict Equality Comparison x === y.
If x is null and y is undefined, return true.
If x is undefined and y is null, return true.
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).
If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.
Return false.
So rather than coercing one side and then the other, or something like that, it's more that the interpreter goes through that list above until it finds a matching condition, and executes the resulting command, which may involve coercing only the left side, or only the right side (and, rarely, both, in case a recursive command is reached, such as with true == '1'
, which will fulfill condition 8, turn into 1 == '1'
, fulfilling condition 6 and turning into 1 == 1
, fulfilling condition 3 and resolving to true
)
edited Feb 7 at 6:47
answered Feb 7 at 6:35
CertainPerformanceCertainPerformance
96.3k165786
96.3k165786
1
@SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?
– Kaiido
Feb 7 at 6:42
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
Feb 7 at 6:47
@SalmanA not yet, but in the next occurence ofthe comparison ToNumber(x) == y.
it may be no?.
– Kaiido
Feb 7 at 6:47
2
@SalmanA 8 calls the==
process recursively, so I'm pretty sure it's possible? Considertrue == '1'
– CertainPerformance
Feb 7 at 6:48
@CertainPerformance yes, your edit makes it clear.
– Salman A
Feb 7 at 6:51
add a comment |
1
@SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?
– Kaiido
Feb 7 at 6:42
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
Feb 7 at 6:47
@SalmanA not yet, but in the next occurence ofthe comparison ToNumber(x) == y.
it may be no?.
– Kaiido
Feb 7 at 6:47
2
@SalmanA 8 calls the==
process recursively, so I'm pretty sure it's possible? Considertrue == '1'
– CertainPerformance
Feb 7 at 6:48
@CertainPerformance yes, your edit makes it clear.
– Salman A
Feb 7 at 6:51
1
1
@SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?
– Kaiido
Feb 7 at 6:42
@SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?
– Kaiido
Feb 7 at 6:42
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
Feb 7 at 6:47
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
Feb 7 at 6:47
@SalmanA not yet, but in the next occurence of
the comparison ToNumber(x) == y.
it may be no?.– Kaiido
Feb 7 at 6:47
@SalmanA not yet, but in the next occurence of
the comparison ToNumber(x) == y.
it may be no?.– Kaiido
Feb 7 at 6:47
2
2
@SalmanA 8 calls the
==
process recursively, so I'm pretty sure it's possible? Consider true == '1'
– CertainPerformance
Feb 7 at 6:48
@SalmanA 8 calls the
==
process recursively, so I'm pretty sure it's possible? Consider true == '1'
– CertainPerformance
Feb 7 at 6:48
@CertainPerformance yes, your edit makes it clear.
– Salman A
Feb 7 at 6:51
@CertainPerformance yes, your edit makes it clear.
– Salman A
Feb 7 at 6:51
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.
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%2f54567524%2fcoercion-in-javascript%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
4
developer.mozilla.org/en-US/docs/Web/JavaScript/…
– VLAZ
Feb 7 at 6:33
5
@adiga definitely not a duplicate. While both questions are about type coercion, this one asks which operand get coerced into the other. The other one is about the source of truth when evaluating the coerced types
– molamk
Feb 7 at 6:41
2
@adiga Its not a dupe. Marked link is checking equality and this post is asking the process of equality. Its like Why 1 == true is true vs How 1 == true is true
– Rajesh
Feb 7 at 6:41
2
@Rajesh It's a possible duplicate. They are related. It's useful future users (and OP) who come to this question and might want to read the linked question.
– adiga
Feb 7 at 6:45
2
@adiga: Duplicates are "duplicate questions", not "related questions with similar answers". So it's definitely not a duplicate.
– Eric Duminil
Feb 7 at 8:41