All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S1533.html Maven / Gradle / Ivy

There is a newer version: 10.17.0.28100
Show newest version

Why is this an issue?

The JavaScript wrapper objects Number, String, and Boolean provide a way to work with their respective primitive types (number, string and boolean) as objects.

Using wrapper can lead to unexpected behavior due to the differences in how they are compared and used in operations compared to primitive types. It can also lead to unnecessary memory allocation and slower code execution.

let x = new Number("0"); // Noncompliant: x is an object, not a primitive
if (x) {
  alert('hi');  // Shows 'hi'.
}

Remove the new keyword to get the primitive value instead of a wrapper object.

let x = Number("0");
if (x) {
  alert('hi');
}

However, it is generally recommended to use primitive types directly instead of wrapper objects, which makes the code more consistent and easier to understand.

let x = 0;
if (x) {
  alert('hi');
}

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy