Skip to content

Unable to reassign negative zero object property to positive zero #27784

Closed
@dlevs

Description

@dlevs
  • Version: v12.2.0
  • Platform: Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64 x86_64

Some object properties cannot be assigned to 0 if they currently have a value of -0, without being first set to a non-zero value.

const duration = {
	years: -0,
	months: -0,
	weeks: -0,
	days: -0,
	hours: -0,
	minutes: -0,
	seconds: -0,
	milliseconds: -0
};

Object.keys(duration).forEach(key => {
	duration[key] = 0;
});

console.log(duration);
/* ✅ For node v11.11.0, all values on `duration` are logged as positive 0.
 * ❌ For node v12.2.0, the logged output is consistently as follows:
 * {
 *   years: 0,
 *   months: 0,
 *   weeks: 0,
 *   days: -0,
 *   hours: -0,
 *   minutes: -0,
 *   seconds: -0,
 *   milliseconds: -0
 * }
 */

// Expected behaviour can be achieved by changing the value to a non-zero value first:
Object.keys(duration).forEach(key => {
	duration[key] = 1;
	duration[key] = 0;
});

console.log(duration);
// ✅ Correct for both v11.11.0 and v12.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.v8 engineIssues and PRs related to the V8 dependency.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions