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 ?
javascript coercion
add a comment |
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 ?
javascript coercion
2
developer.mozilla.org/en-US/docs/Web/JavaScript/…
– VLAZ
1 hour ago
Possible duplicate of Is true == 1 and false == 0 in JavaScript?
– adiga
1 hour ago
3
@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
56 mins ago
1
@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
56 mins ago
1
@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
52 mins ago
add a comment |
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 ?
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 ?
javascript coercion
javascript coercion
edited 13 mins ago
tomerpacific
769322
769322
asked 1 hour ago
ScipionScipion
2,76343580
2,76343580
2
developer.mozilla.org/en-US/docs/Web/JavaScript/…
– VLAZ
1 hour ago
Possible duplicate of Is true == 1 and false == 0 in JavaScript?
– adiga
1 hour ago
3
@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
56 mins ago
1
@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
56 mins ago
1
@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
52 mins ago
add a comment |
2
developer.mozilla.org/en-US/docs/Web/JavaScript/…
– VLAZ
1 hour ago
Possible duplicate of Is true == 1 and false == 0 in JavaScript?
– adiga
1 hour ago
3
@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
56 mins ago
1
@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
56 mins ago
1
@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
52 mins ago
2
2
developer.mozilla.org/en-US/docs/Web/JavaScript/…
– VLAZ
1 hour ago
developer.mozilla.org/en-US/docs/Web/JavaScript/…
– VLAZ
1 hour ago
Possible duplicate of Is true == 1 and false == 0 in JavaScript?
– adiga
1 hour ago
Possible duplicate of Is true == 1 and false == 0 in JavaScript?
– adiga
1 hour ago
3
3
@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
56 mins ago
@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
56 mins ago
1
1
@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
56 mins ago
@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
56 mins ago
1
1
@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
52 mins ago
@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
52 mins ago
add a comment |
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
55 mins ago
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
50 mins ago
@SalmanA not yet, but in the next occurence ofthe comparison ToNumber(x) == y.
it may be no?.
– Kaiido
49 mins ago
1
@SalmanA 8 calls the==
process recursively, so I'm pretty sure it's possible? Considertrue == '1'
– CertainPerformance
49 mins ago
@CertainPerformance yes, your edit makes it clear.
– Salman A
46 mins ago
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
55 mins ago
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
50 mins ago
@SalmanA not yet, but in the next occurence ofthe comparison ToNumber(x) == y.
it may be no?.
– Kaiido
49 mins ago
1
@SalmanA 8 calls the==
process recursively, so I'm pretty sure it's possible? Considertrue == '1'
– CertainPerformance
49 mins ago
@CertainPerformance yes, your edit makes it clear.
– Salman A
46 mins ago
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
55 mins ago
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
50 mins ago
@SalmanA not yet, but in the next occurence ofthe comparison ToNumber(x) == y.
it may be no?.
– Kaiido
49 mins ago
1
@SalmanA 8 calls the==
process recursively, so I'm pretty sure it's possible? Considertrue == '1'
– CertainPerformance
49 mins ago
@CertainPerformance yes, your edit makes it clear.
– Salman A
46 mins ago
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 50 mins ago
answered 1 hour ago
CertainPerformanceCertainPerformance
85k154169
85k154169
1
@SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?
– Kaiido
55 mins ago
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
50 mins ago
@SalmanA not yet, but in the next occurence ofthe comparison ToNumber(x) == y.
it may be no?.
– Kaiido
49 mins ago
1
@SalmanA 8 calls the==
process recursively, so I'm pretty sure it's possible? Considertrue == '1'
– CertainPerformance
49 mins ago
@CertainPerformance yes, your edit makes it clear.
– Salman A
46 mins ago
add a comment |
1
@SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?
– Kaiido
55 mins ago
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
50 mins ago
@SalmanA not yet, but in the next occurence ofthe comparison ToNumber(x) == y.
it may be no?.
– Kaiido
49 mins ago
1
@SalmanA 8 calls the==
process recursively, so I'm pretty sure it's possible? Considertrue == '1'
– CertainPerformance
49 mins ago
@CertainPerformance yes, your edit makes it clear.
– Salman A
46 mins ago
1
1
@SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?
– Kaiido
55 mins ago
@SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?
– Kaiido
55 mins ago
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
50 mins ago
@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).
– Salman A
50 mins ago
@SalmanA not yet, but in the next occurence of
the comparison ToNumber(x) == y.
it may be no?.– Kaiido
49 mins ago
@SalmanA not yet, but in the next occurence of
the comparison ToNumber(x) == y.
it may be no?.– Kaiido
49 mins ago
1
1
@SalmanA 8 calls the
==
process recursively, so I'm pretty sure it's possible? Consider true == '1'
– CertainPerformance
49 mins ago
@SalmanA 8 calls the
==
process recursively, so I'm pretty sure it's possible? Consider true == '1'
– CertainPerformance
49 mins ago
@CertainPerformance yes, your edit makes it clear.
– Salman A
46 mins ago
@CertainPerformance yes, your edit makes it clear.
– Salman A
46 mins ago
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
2
developer.mozilla.org/en-US/docs/Web/JavaScript/…
– VLAZ
1 hour ago
Possible duplicate of Is true == 1 and false == 0 in JavaScript?
– adiga
1 hour ago
3
@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
56 mins ago
1
@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
56 mins ago
1
@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
52 mins ago