Explicitness

Perform type coercion at the beginning of the statement.

No Primitive Wrapper Instances

ESlint: no-new-wrappers

Strings

Examples

Incorrect code for this rule:

this.snowflakes = 20;

// Type of "snowflakes" is "object" not "string".
const snow = new String(this.snowflakes);
this.snowflakes = 20;

// Invokes "this.reviewScore.valueOf()".
const snow = this.snowflakes + "";
this.snowflakes = 20;

// Not guaranteed to return a string.
const snow = this.snowflakes.toString();

Correct code for this rule:

this.snowflakes = 20;
const snow = String(this.snowflakes);

Numbers

Use Number for type casting and parseInt always with a radix for parsing strings.

ESLint: radix and no-new-wrappers

Examples

Incorrect code for this rule:

const snowflakes = "4";
const snow = new Number(snowflakes);
const snowflakes = "4";
const snow = +snowflakes;
const snowflakes = "4";
const snow = snowflakes >> 0;
const snowflakes = "4";
const snow = parseInt(snowflakes);

Correct code for this rule:

const snowflakes = "4";
const snow = Number(snowflakes);
const snowflakes = "4";
const snow = parseInt(snowflakes, 10);

Booleans

ESLint: no-new-wrappers

Examples

Incorrect code for this rule:

const snowflakes = 0;
const hasFlakes = new Boolean(snowflakes);

Correct code for this rule:

const snowflakes = 0;
const hasFlakes = Boolean(snowflakes);

Recommended code for this rule:

const snowflakes = 0;
const hasFlakes = !!snowflakes;

Bitshift

Be careful when using bitshift operations! Numbers are represented as 64-bit values, but bitshift operations always return a 32-bit integer. Bitshift can lead to unexpected behavior for integer values larger than 32 bits. The largest signed 32-bit integer is 2147483647 (2,147,483,647).

2147483647 >> 0; // 2147483647
2147483648 >> 0; // -2147483648
2147483649 >> 0; // -2147483647

When using Bitshift instead of parseInt for performance reasons, make sure to clarify and describe the design decision in the code documentation.

/**
 * This assignment has been performance optimized.
 * It uses bitshifting instead of casting via the "parseInt" method.
 * Bitshifting the String to coerce it to a Number resulted in a performance boost of almost 90%.
 */
const snow = snowflakes >> 0;
References

results matching ""

    No results matching ""