Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Local Storage Project

What is the difference between > -1 and > 0 in JavaScript.

I'm guessing > -1 equates to greater than 0 and > 0 equates to greater than 1, so basically it does the same thing. But i might be wrong here.

Thank you!

3 Answers

Steven Parker
Steven Parker
231,127 Points

This is really a math issue, it's not unique to Javascript and I would think any language you program in would handle this the same way. But mathematically, these are very different tests.

Assume we're talking about integers, a test for "> 0" would include all positive numbers, but a test for "> -1" would include all positive numbers and zero.

If you're working with floats, a test for "> 0" is still a test for positive numbers, but a test for "> -1" would also include both zero and any fractional negative number (such as -0.4 or -0.998).

Thank you for answering!

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Sjoerd,

No, they actually do not do the same thing.

In your question, you state that "> -1 equates to greater than 0", while that is partially true, it will also include zero. So, it would return true for 0, 1, 2, 3, 4, ... etc.
Now with > 0, it will be the same thing. Being "greater than 1" is only partially correct, as it will include 1 (but not zero). So, this would return 1, 2, 3, 4, 5, ... etc.

In short, these are two very different evaluations.

I hope this clears it up for you. :) :dizzy:

Thank you for clearing that up Jason!

Hm, I thought it was because the first item in an array has an index value of 0, and so if str is already there, it would have an index value of 0 or more.