3

In _.each method of underscore.js source code, they have used obj.length === +obj.length. In the first else if condition. Why they have used this + operator, whats is the significance of it?

var each = _.each = _.forEach = function(obj, iterator, context) {
if (obj == null) return;
if (nativeForEach && obj.forEach === nativeForEach) {
  obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
  for (var i = 0, l = obj.length; i < l; i++) {
    if (iterator.call(context, obj[i], i, obj) === breaker) return;
  }
} else {
  for (var key in obj) {
    if (_.has(obj, key)) {
      if (iterator.call(context, obj[key], key, obj) === breaker) return;
    }
  }
}

};

Devang Paliwal
  • 291
  • 2
  • 4
  • possible duplicate of [Single plus operator in javascript](http://stackoverflow.com/questions/14470973/single-plus-operator-in-javascript) – Oliver Jun 07 '13 at 08:23
  • possible duplicate of [obj.length === +obj.length in javascript](http://stackoverflow.com/questions/9188998/obj-length-obj-length-in-javascript) – Yeonho Feb 21 '14 at 06:03

3 Answers3

6

The + operator converts its argument into a number. They then use the "is exactly the same as" operator === to test obj.length against the result.

What it actually means is that the test will only succeed if obj.length is a number, rather than (for example) the string "3".

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
3

The unary + operator will coerce a string to a number so that the faster === operator may be used against the left-hand argument which is known to be a number.

e.g.

s = "123";
i = 123;

i === s; // false
i === +s; // true
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • the use of `+` here is more efficient than `parseInt()` ? – MaVRoSCy Jun 06 '13 at 05:52
  • @MaVRoSCy: I don't know. I haven't profiled the code. It might be more of a style choice. It's certainly shorter than writing `parseInt(s, 16)` [which is recommended when you may be parsing a number that looks octal](http://fczaja.blogspot.com/2010/09/javascript-parseint-gotcha.html). – Mark Rushakoff Jun 06 '13 at 06:00
  • @MarkRushakoff: 16? Octal? – RichieHindle Jun 06 '13 at 06:13
  • @RichieHindle: Yes, such as the issue in [this question](http://stackoverflow.com/questions/850341/how-do-i-work-around-javascripts-parseint-octal-behavior). – Mark Rushakoff Jun 06 '13 at 06:14
  • @MarkRushakoff: Yes, but don't you mean `10` rather than `16`? Passing `16` would parse as hex. – RichieHindle Jun 06 '13 at 06:48
  • 1
    @RichieHindle *facepalm* Agh! Sorry I didn't understand you the first time. Looks like I should have gone to bed an hour ago :P – Mark Rushakoff Jun 06 '13 at 06:56
3
obj.length can be any type even undefined. 
+obj.length is always a number.

So this code checks if the length property exists and is a number. The reason for this check is that _.each() accepts both arrays and non-array objects. In case of an array the length property is necessary to iterate over its elements while a for..in loop is the way to go in case of a non-array object.

Rinku
  • 1,078
  • 6
  • 11