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?
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.
Looked quite obvious to me.
LikeLike
wat
LikeLike
It works because array[“0”] works through implicit casting. Maybe it is obvious but it’s also probably not such a good idea.
LikeLike
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?
LikeLike
length is a property of array so it’s doing array.length or array[‘length’] and assigning the value to the variable l.
LikeLike
With all these odd corner cases, JavaScript isn’t anywhere near as easy as so many people claims it to be.
LikeLike
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.
LikeLike