WTF JS

JS… wszyscy wiemy jak zbudowany jest ten język i ile jest w nim dziwnych i nielogicznych rzeczy. Postaram się opisać i wytłumaczyć choć część porąbanych rzeczy jakich możemy uświadczyć w JSie.

Bartek Legięć

Bartek Legięć

„WTF JS?” [PL]

2018-02-14

@bibix1999

Adding values

        
          "3" - 2
        
      

Adding values

        
          "3" + 2
        
      

Adding values

        [] + {}
        [].toString() + {}.toString()
        "" + "[object Object]"
        "[object Object]"
      

Adding values

        {} + []
        + []
        0
      

Adding values

        [1, 2, 3] + [4, 5, 6]
        [1, 2, 3].toString() + [4, 5, 6].toString()
        "1,2,3" + "4,5,6"
        "1,2,34,5,6"
      

Multiplying strings

        ""*""
        ""**""
      

MIN_VALUE

          Number.MIN_VALUE > 0
          Number.MIN_VALUE // -> 5e-324 (5 * 10^-324)
        

parseInt

        parseInt("foo")
        parseInt("foo", 16)
      

parseInt

        parseInt("Infinity")
        parseInt("Infinity", 19)
        parseInt("Infinity", 24) // -> 151176378
        parseInt("Infinity", 29) // -> 385849803
        parseInt("Infinity", 36) // -> 1461559270678
        parseInt("Infinity", 37) // -> NaN
      

Null comparison

            
              null > 0
            
            
            
        

null > 0

            null > 0
            +null > +0
            0 > 0
            false
        

Null comparison

            
              null > 0
              null == 0
            
             
        

null == 0

            null == 0
            +null == +0
            0 == 0
            true
            // it should be like this right?
        

null == 0

Null comparison

            
              null > 0
              null == 0
              null >= 0
            
        

null >= 0

            null >= 0
            !(null < 0)
            !(+null < +0)
            !(0 < 0)
            !false
            true
        

NaN comparison

            1337 === 1337
            NaN === NaN
        

NaN === NaN

        

1. If Type(x) is different from Type(y), return false.

2. If Type(x) is Number, then

    a. If x is NaN, return false.

    b. If y is NaN, return false.

Array comparison

        
          [] == []
        
      

Array comparison

          [] == ![]
          [] == !true
          [] == false
          +[] == +false
          0 == 0
          true
        

Array comparison

        [] == ""
        [].toString() == ""
        "" == ""
        true
      

Array comparison

        [0] == 0
        [0].toString() == 0
        "0" == 0
        +"0" == 0
        0 === 0
        true
      

Array comparison

        [[[[[[]]]]]] == 0
        [[[[[[].toString()]]]]] == 0
        [[[[[""].toString()]]]] == 0
        [[[[""].toString()]]] == 0
        [[[""].toString()]] == 0
        [[""].toString()] == 0
      

Array comparison

        [[""].toString()] == 0
        [""].toString() == 0
        "" == 0
        +"" == 0
        0 == 0
        true
      

Array comparison

        ![] === ![]
        !true === !true
        false === false
        true
      

Array comparison

        ["a","b","c"][3,2,1]
        ["a","b","c"][(3,2,1)]
        ["a","b","c"][1]
        "b"
      

map

        ["1", "2", "3"].map(parseInt)
        ["1", "2", "3"].map((...args) => parseInt(...args))
        ["1", "2", "3"].map((val, i) => parseInt(val, i))
        // [0] parseInt("1", 0) -> 1
        // [1] parseInt("2", 1) -> NaN
        // [2] parseInt("3", 2) -> NaN
      

sort

        
          [10, 9, 8, 3, 2, 1, 0].sort()
        
      

match

        "[]\\`".match(/[A-z]/g)
      

ASCII table

65  A 66  B 67  C ... 84  T 85  U 86  V 87  W 88  X 89  Y 90  Z 91  [ 92  \ 93  ] 94  ^ 95  _ 96  ` 97  a 98  b 99  c 100  d 101  e 102  f 103  g ... 120  x 121  y 122  z

return

        
          function foo() {
            return
            {
              bar: "hi"
            };
          }
        
      

return

        
          function foo() {
            return;
            {
              bar: "hi"
            };
          }
        
      

Is it possible?

        
          if (a == 0 && a == 1 && a == 2) {
            console.log("Hi!");
          }
        
      

Is it possible?

        
          const a = {
            i: 0,
            toString: function() {
              return this.i++;
            }
          }
        
      

Sources