Coercion in JavaScript












7















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 ?










share|improve this question




















  • 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
    1 hour 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
    1 hour 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
    1 hour ago


















7















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 ?










share|improve this question




















  • 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
    1 hour 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
    1 hour 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
    1 hour ago
















7












7








7


1






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 ?










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 ?







javascript coercion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 22 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
    1 hour 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
    1 hour 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
    1 hour ago
















  • 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
    1 hour 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
    1 hour 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
    1 hour 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
1 hour 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
1 hour 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
1 hour 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
1 hour 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
1 hour 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
1 hour ago














1 Answer
1






active

oldest

votes


















8














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
    1 hour ago













  • @Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).

    – Salman A
    59 mins ago













  • @SalmanA not yet, but in the next occurence of the comparison ToNumber(x) == y. it may be no?.

    – Kaiido
    58 mins ago






  • 1





    @SalmanA 8 calls the == process recursively, so I'm pretty sure it's possible? Consider true == '1'

    – CertainPerformance
    58 mins ago













  • @CertainPerformance yes, your edit makes it clear.

    – Salman A
    54 mins ago











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









8














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
    1 hour ago













  • @Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).

    – Salman A
    59 mins ago













  • @SalmanA not yet, but in the next occurence of the comparison ToNumber(x) == y. it may be no?.

    – Kaiido
    58 mins ago






  • 1





    @SalmanA 8 calls the == process recursively, so I'm pretty sure it's possible? Consider true == '1'

    – CertainPerformance
    58 mins ago













  • @CertainPerformance yes, your edit makes it clear.

    – Salman A
    54 mins ago
















8














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
    1 hour ago













  • @Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).

    – Salman A
    59 mins ago













  • @SalmanA not yet, but in the next occurence of the comparison ToNumber(x) == y. it may be no?.

    – Kaiido
    58 mins ago






  • 1





    @SalmanA 8 calls the == process recursively, so I'm pretty sure it's possible? Consider true == '1'

    – CertainPerformance
    58 mins ago













  • @CertainPerformance yes, your edit makes it clear.

    – Salman A
    54 mins ago














8












8








8







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 58 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
    1 hour ago













  • @Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).

    – Salman A
    59 mins ago













  • @SalmanA not yet, but in the next occurence of the comparison ToNumber(x) == y. it may be no?.

    – Kaiido
    58 mins ago






  • 1





    @SalmanA 8 calls the == process recursively, so I'm pretty sure it's possible? Consider true == '1'

    – CertainPerformance
    58 mins ago













  • @CertainPerformance yes, your edit makes it clear.

    – Salman A
    54 mins ago














  • 1





    @SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?

    – Kaiido
    1 hour ago













  • @Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).

    – Salman A
    59 mins ago













  • @SalmanA not yet, but in the next occurence of the comparison ToNumber(x) == y. it may be no?.

    – Kaiido
    58 mins ago






  • 1





    @SalmanA 8 calls the == process recursively, so I'm pretty sure it's possible? Consider true == '1'

    – CertainPerformance
    58 mins ago













  • @CertainPerformance yes, your edit makes it clear.

    – Salman A
    54 mins ago








1




1





@SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?

– Kaiido
1 hour ago







@SalmanA I may read it incorrectly but in 8 couldn't it be both? This algo calls itself right?

– Kaiido
1 hour ago















@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).

– Salman A
59 mins ago







@Kaiido in 8, y is not coerced. Likewise in 9 (where x is not coerced).

– Salman A
59 mins ago















@SalmanA not yet, but in the next occurence of the comparison ToNumber(x) == y. it may be no?.

– Kaiido
58 mins ago





@SalmanA not yet, but in the next occurence of the comparison ToNumber(x) == y. it may be no?.

– Kaiido
58 mins ago




1




1





@SalmanA 8 calls the == process recursively, so I'm pretty sure it's possible? Consider true == '1'

– CertainPerformance
58 mins ago







@SalmanA 8 calls the == process recursively, so I'm pretty sure it's possible? Consider true == '1'

– CertainPerformance
58 mins ago















@CertainPerformance yes, your edit makes it clear.

– Salman A
54 mins ago





@CertainPerformance yes, your edit makes it clear.

– Salman A
54 mins ago


















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

flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

Mangá

 ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕