Coercion in JavaScript












15















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"










share|improve this question




















  • 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
















15















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"










share|improve this question




















  • 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














15












15








15


4






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"










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












1 Answer
1






active

oldest

votes


















27














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:




  1. If Type(x) is the same as Type(y), then return the result of performing Strict Equality Comparison x === y.


  2. If x is null and y is undefined, return true.


  3. If x is undefined and y is null, return true.


  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).


  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.


  6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.


  7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).


  8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).


  9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.


  10. 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)






share|improve this answer





















  • 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 of the 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? Consider true == '1'

    – CertainPerformance
    Feb 7 at 6:48













  • @CertainPerformance yes, your edit makes it clear.

    – Salman A
    Feb 7 at 6:51












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
});


}
});














draft saved

draft discarded


















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









27














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:




  1. If Type(x) is the same as Type(y), then return the result of performing Strict Equality Comparison x === y.


  2. If x is null and y is undefined, return true.


  3. If x is undefined and y is null, return true.


  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).


  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.


  6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.


  7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).


  8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).


  9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.


  10. 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)






share|improve this answer





















  • 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 of the 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? Consider true == '1'

    – CertainPerformance
    Feb 7 at 6:48













  • @CertainPerformance yes, your edit makes it clear.

    – Salman A
    Feb 7 at 6:51
















27














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:




  1. If Type(x) is the same as Type(y), then return the result of performing Strict Equality Comparison x === y.


  2. If x is null and y is undefined, return true.


  3. If x is undefined and y is null, return true.


  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).


  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.


  6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.


  7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).


  8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).


  9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.


  10. 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)






share|improve this answer





















  • 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 of the 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? Consider true == '1'

    – CertainPerformance
    Feb 7 at 6:48













  • @CertainPerformance yes, your edit makes it clear.

    – Salman A
    Feb 7 at 6:51














27












27








27







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:




  1. If Type(x) is the same as Type(y), then return the result of performing Strict Equality Comparison x === y.


  2. If x is null and y is undefined, return true.


  3. If x is undefined and y is null, return true.


  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).


  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.


  6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.


  7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).


  8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).


  9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.


  10. 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)






share|improve this answer















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:




  1. If Type(x) is the same as Type(y), then return the result of performing Strict Equality Comparison x === y.


  2. If x is null and y is undefined, return true.


  3. If x is undefined and y is null, return true.


  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).


  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.


  6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.


  7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).


  8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).


  9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.


  10. 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)







share|improve this answer














share|improve this answer



share|improve this answer








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 of the 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? Consider true == '1'

    – CertainPerformance
    Feb 7 at 6:48













  • @CertainPerformance yes, your edit makes it clear.

    – Salman A
    Feb 7 at 6:51














  • 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 of the 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? Consider true == '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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Human spaceflight

Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

張江高科駅