7 thoughts on “Non-obvious

  1. So length is casting a string to an integer and assigning it to l?

    Of which string is it taking the length? Is it “bar” because the other two values have already been assigned? Or is it “foo” because implicit casting is occurring on the 0th array element?

    Like

  2. The JavaScript array is a gift that keeps giving. Combined with the new destructuring syntax yes the edge cases are pretty wacked. That said, nobody actually uses the language this way.

    There are some odd idioms that you find in the code that exploit edge cases but they tend to be few and far between.

    Examples:

    !!x to cast a truthy value to a boolean.

    +x to cast a string containing a number into a number

    x || 32 to replace a falsy number with a default

    x && x.y && x.y.z to safely test a nested value.

    Less appealing is

    !~x.indexOf(“a”);

    Which returns true if indexOf doesn’t return -1 (as per Java). These days people use includes.

    Like

Leave a comment