whyyie
whyyie

Reputation: 792

Result of -0 subtracted from +0

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)

Upvotes: 1

Views: 101

Answers (1)

Ry-
Ry-

Reputation: 224942

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.

Upvotes: 5

Related Questions