Skip to content

Latest commit

 

History

History
58 lines (42 loc) · 1.64 KB

prefer-math-min-max.md

File metadata and controls

58 lines (42 loc) · 1.64 KB

Prefer Math.min() and Math.max() over ternaries for simple comparisons

💼 This rule is enabled in the ✅ recommended config.

🔧 This rule is automatically fixable by the --fix CLI option.

This rule enforces the use of Math.min() and Math.max() functions instead of ternary expressions when performing simple comparisons, such as selecting the minimum or maximum value between two or more options.

By replacing ternary expressions with these functions, the code becomes more concise, easier to understand, and less prone to errors. It also enhances consistency across the codebase, ensuring that the same approach is used for similar operations, ultimately improving the overall readability and maintainability of the code.

Examples

height > 50 ? 50 : height; // ❌
Math.min(height, 50); // ✅
height >= 50 ? 50 : height; // ❌
Math.min(height, 50); // ✅
height < 50 ? height : 50; // ❌
Math.min(height, 50); // ✅
height <= 50 ? height : 50; // ❌
Math.min(height, 50); // ✅
height > 50 ? height : 50; // ❌
Math.max(height, 50); // ✅
height >= 50 ? height : 50; // ❌
Math.max(height, 50); // ✅
height < 50 ? 50 : height; // ❌
Math.max(height, 50); // ✅
height <= 50 ? 50 : height; // ❌
Math.max(height, 50); // ✅