The type conversion of some eccentricities

collected some strange type conversions. I don"t know why. Let"s discuss why?

[] == ![]         // true
"1" == true       // true
"2" == true       // false
"3" == true       // false
null >= 0         // true
{} + 1            // 1
{var a = 10} + 1  // 1
{} + {}           // "[object Object][object Object]"
+[]               // 0
{} + []           // 0
[1,2]+ [3,4]      // "1,23,4"
+[2]              // 2
[2]+1             //"21"
[2] + (-1)        // "2-1"

null Special case

+null        // 0
null == 0    // false
null > 0     // false
null < 0     // false

!!null         // false
null == false  // false
null == true   // false
Apr.14,2022

use = = to compare. If the type of comparison is different, it can be divided into four cases
1. The equality comparison between a string and a number, where the string is forced to be converted to a number
2. For equality comparisons between other types and Boolean types, Boolean values are forcibly converted to numbers, true is converted to 1 strong false to 0
3. Equality comparison between null and undefined, null = = undefined returns true
4. Equality comparison between objects and non-objects. Objects need to perform ToPrimitive abstract operations (such as toString (), valueOf ())

according to the above four rules, understand the following questions:

  1. [] = =! [] equals [] = = false equals''= = false equals''= 0 equals 0 = 0 so the result is true
  2. '1' = = true is equivalent to'1' = = 1 is equivalent to 1, so the result is true
  3. '2' = = true equals'2' = = 1 equals 2 = = 1, so the result is false
  4. '3' = = true equals'3' = = 1 equals 3 = = 1, so the result is false

according to the specification a < = b is treated as b , so

  1. null > = 0 can be understood as! (null < 0). Because null < 0 is false, null > = 0 results in true

when using the + operator, if one of the operators is an object, the valueOf () method is called first, and if the method returns a primitive type value, the toString () method is no longer called. Otherwise, the toString () method is called (with an exception, the instance object of Date always calls toString before valueOf). The valueOf () of the
array does not get a primitive type value, so the array calls the toString () method.

    The {} in front of
  1. {} + 1 is an empty block of code, which might be easier to understand if you add the switch button. {} enter + 1. + 1 results return 1.
  2. {var a = 10} + 1 follows the above. The first {} is a code block, but there is no enter key added after it. The + 1 result returns 1.
  3. {} + {} is the addition of two empty objects, divided into strings ({}). ToString (), and then added. The result is "object Object"
  4. + [] is equivalent to Number ([]), and the result is 0
  5. {} + [] where {} is still an empty code block, so the result of + [] is returned, that is, 0
  6. [1pr 2] + [3je 4] [1pr 2] becomes the string '1je 2' [3pr 4] and becomes the string '3je 4', so the result is "1 mei 23 mei 4"
  7. + [2] equals +'2' equals Number ('2') so the result is 2
  8. [2] + 1 equals'2' + 1, so the result is'21'
  9. [2] + (- 1) equals' 2percent + (- 1), so the result is'2-1'

[] =! [] = > [] = = false (array inverted) = >''= = false ([] convert original value) = > 0 = = 0 (call Number conversion)
'1' = = ture = > 1 = = 1 (call Number)
'2' = = true = > 2 = = 1 (call Number)
'3' = = true = > 3 = = 1 (call Number)
null > = 0 = > 0 > > = 0 (call Number)
{} + 1 = > 1 = > 1 (the previous {} is considered to be a Empty code blocks)
{var a = 10} + 1 = > + 1 (similar to "+ 1" above)
{} + {} = > [object Object] + [object Object] (left and right sides are considered as objects to call toString () and add)

  • [] = > + 0 (call Number)

{} + [] = > + [] ("{}" is considered to be an empty code block) = > + 0 (call Number)
[1Mague 2] + [3jue 4] = > '1Perry 2' + '3jue 4' (call toString)

  • [2] = > + 2 (call Number)

[2] + 1 = >'2' + 1 (call toString for finding the original value first) = > '21' (when string is converted to string first)
[2] + (- 1) = >' 2' +-1 (call toString for the original value first) = >'2-1' (when string is converted to string first)


when I first learned js, these places were gnawing and gnawing.

later found that in practical work, it is good to know these things.

however, in actual projects, you should try your best to avoid using implicit conversions when comparing'='.

basically, only implicit conversions of true and false values are used more frequently, such as if (a). When comparing, they are all compared by'='to avoid unexpected results caused by'='.

moreover, today's more stringent projects use ts, mandatory type declarations, and rarely encounter implicit conversions in comparison.


Kong Yiji looked very happy, tapped the long fingernails of his two fingers on the counter, nodded and said, "that's right!"... There are four ways to write a reply, do you know? "

< hr >

Don't mislead people. Study these useless things every day
another person has already said that in practical engineering practice, in addition to the implicit conversion of true and false values, other implicit conversions should be avoided. If other implicit conversions are widely used in addition to the implicit conversion of true and false values, it will only seriously reduce the readability of the code, increase the difficulty of collaboration and the bug rate, and may also be beaten by colleagues
to write this kind of code at work. To put it bluntly, it is called finding and pumping

< hr >

js's pit, it is enough to know where it is and how to avoid it. It is completely possible not to know what the pit looks like.
for beginners, before they have enough programming abstract ability to solve practical problems, they don't improve their programming ability. Instead, they put the cart before the horse every day in front of all kinds of grammatical details, and even study the pit part of the language. Spending more time writing code and solving practical problems is the right direction (as long as it's not stupid, grammar and everything can be mastered naturally)

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1be68be-3104e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1be68be-3104e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?