Here is a line of code from underscore. What is that plus prefix for in this line?
if (obj.length === +obj.length) { // plus prefix?
Adding a + symbol effectively converts a variable into a number, such that:
+"1" === 1;
However, please note that
+"1" === "1"; // FALSE
+"1" == "1"; // TRUE
This is because == will convert its operands to the same type, whereas === will not.
That means that the test:
obj.length === +obj.length
Is essentially trying to test whether obj.length is numeric.
In Underscore, this code is trying to figure out if a variable of unknown type has a property called length and whether it is numeric. The assumption is that, if these are both true, you can iterate over the variable is if it were an array.
Please note, the OP's code has a number of bugs in it, not least of which is this approach to detecting if something is an Array (or Arraylike). The following object would cause problems:
var footballField = {
covering: "astroturf",
condition: "muddy",
length: 100
};
I'm not advocating the above approach... just explaining someone else's.