org.sonar.l10n.javascript.rules.javascript.S1533.html Maven / Gradle / Ivy
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
- MDN web docs - Primitive
- MDN web docs -
Number()
constructor
- MDN web docs -
String()
constructor
- MDN web docs -
Boolean()
constructor