1

I came across a statement "If -0 is subtracted from +0, the result is -0" in a JavaScript book published in year 2012.

However, when I compute +0 - (-0) in browser, it returns 0 instead of -0. I would like to know whether there is a change in ECMAScript since then or is it just simply an error/typo in the book.

If what the book mentioned is true, I would like to hear explanation and elaboration on this part.

Book: Professional JavaScript for Web Developers, 3rd Ed. by Nicholas C. Zakas (Chapter 3 - pg 63)

whyyie
  • 792
  • 3
  • 10
  • I don't understand why you need -0 or +0. Will it have any implications on the calculation as 0 is just 0 how can it be negative or positive. – RAJA THEVAR Jan 09 '17 at 15:53
  • even if it is internally right, it makes no sense, bacause you can not display it. – Nina Scholz Jan 09 '17 at 15:53
  • 1
    What book are you talking about? And which chapter? –  Jan 09 '17 at 15:55
  • 2
    you may have a look [here](https://books.google.de/books?id=KW9G9rdlStIC&pg=PA58&lpg=PA58&dq=If+-0+is+subtracted+from+%2B0,+the+result+is+-0&source=bl&ots=4XZSWIZWWt&sig=XRPKN-eUsHgJwzqDlVwNrBsOUeE&hl=de&sa=X&ved=0ahUKEwjGjerBtrXRAhUBGCwKHU72Dc0Q6AEIHjAA#v=onepage&q&f=false) – Nina Scholz Jan 09 '17 at 15:56
  • @Ryan: TIL. Also I did `-0 + +0` instead of `-0 - +0`, which didn't help... – Felix Kling Jan 09 '17 at 15:58
  • @RAJATHEVAR: http://stackoverflow.com/q/27074330/218196 – Felix Kling Jan 09 '17 at 16:02

1 Answers1

5

The book is incorrect. Maybe it meant -0 - +0. From 12.7.5:

  • The sum of two negative zeroes is −0. The sum of two positive zeroes, or of two zeroes of opposite sign, is +0.

Given numeric operands a and b, it is always the case that a–b produces the same result as a +(–b).

and 12.5.0:

The unary - operator converts its operand to Number type and then negates it. Negating +0 produces −0, and negating −0 produces +0.

Also, I skipped to another random page in the book and found this:

Comma Operator

The comma operator allows execution of more than one operation in a single statement, as illustrated here:

var num1=1, num2=2, num3=3;

which is not an instance of the comma operator. Two for two; get a refund.

Community
  • 1
  • 1
Ry-
  • 218,210
  • 55
  • 464
  • 476