0

What does it mean when -1 is used as a return value as seen in the first if statement below?

MyLinkedList.prototype.get = function(i) {
    if (i < 0 || i >= this.length) return -1;
    if (i + 1 === this.length) return this.tail.prev.val;
    let curr = this.head.next;
    while (i--) curr = curr.next;
    return curr.val;
};
hking2
  • 25
  • 3
  • `-1 == false` will never be true, nor will `-1 == NaN`, nor will `-1 == undefined`. At best, this code checks if the JavaScript interpreter is following standards. That, or it's just completely meaningless. – VLAZ Sep 14 '20 at 20:23
  • 2
    "when -1 is used as a return value"...this code never does that. It's unclear how your question relates to the code. – ADyson Sep 14 '20 at 20:24
  • In most cases if you want to signal using an int instead of a bool to say that there are outlying cases, -1 can be an acceptable value. For example, if I return a 1, then I'm implying that the expected condition was true, if I use a 0, it was false. However, in a case where there was an unexpected behavior, I might choose to use -1 to signal that a simple true/false dichotomy wasn't enough to describe that there was an issue. – Carson Sep 14 '20 at 20:24
  • Take a look at: https://stackoverflow.com/questions/35642809/understanding-javascript-truthy-and-falsy – Crisoforo Gaspar Sep 14 '20 at 20:24
  • @Carson not a very good system. You can still just use booleans + `null` to signal the same. – VLAZ Sep 14 '20 at 20:27
  • You could, but not a very good system is some what arbitrary. It's a pretty common setup in academic coding environments. – Carson Sep 14 '20 at 20:28
  • 1
    OP seems to have [completely changed](https://stackoverflow.com/posts/63891346/revisions) the question and code sample a moment ago. Unclear if they posted the wrong thing or just thought better of the question. Any explanation please? – ADyson Sep 14 '20 at 20:29
  • This question no longer appears to have *anything* to do with the original one. – zcoop98 Sep 14 '20 at 20:30
  • 1
    Anyway in this case it seems the -1 is intended to indicate a validation error - specifically (if you read the `if` code) that the input value was outside the allowed range. – ADyson Sep 14 '20 at 20:30
  • @Carson thanks that helps me better understand how -1 is used. – hking2 Sep 14 '20 at 20:31
  • @ADyson yes I added new code because the first code was unclear to people. – hking2 Sep 14 '20 at 20:31
  • @user12170797 well, more than that really, it seems to be completely unrelated to this code, which makes a lot more sense. That's why it seemed a bizarre change, but it has improved the question a lot. – ADyson Sep 14 '20 at 20:33
  • @ADyson thanks, yeah i will be more direct next time I ask a question. the original code was my attempt at testing the bool value of -1, but guess it didn't make much sense. – hking2 Sep 14 '20 at 20:40
  • No it didn't really to be honest. Especially because you asked why it returned -1, when it didn't do that at all! That was quite confusing. Anyway the new question is much better, thanks. – ADyson Sep 14 '20 at 21:09

2 Answers2

5

The numeric literal -1 is technically considered Truthy.

Most functions that send -1 as a return value are these that work with index values or other counting related problems dealing algorithms (example: String.prototype.search())

DevCl9
  • 298
  • 1
  • 9
2

In this case it seems the -1 is intended to indicate a validation error - specifically (if you read the if code) that the input value was outside the allowed range.

This should hopefully indicate to the caller that the value being returned is not a value from a real list item - although of course it's not entirely impossible that -1 could be the value of one of the items in the list. Arguably it would be better to throw an error of some kind, or return null.

ADyson
  • 57,178
  • 14
  • 51
  • 63