TheAlgorithms/JavaScript · error · TypeError
Invalid Triangle sides.
Error message
Invalid Triangle sides.
What it means
Thrown by areaTriangleWithAllThreeSides(side1, side2, side3) as a TypeError when the three given lengths violate the triangle inequality. The library enforces this before applying Heron's formula because Heron's formula would otherwise produce a NaN (square root of a negative product) for impossible triangles. The check is strict: a sum equal to the third side (degenerate triangle) is also rejected. This guard prevents silently returning mathematically meaningless results.
Source
Thrown at Maths/Area.js:93
* @function areaTriangleWithAllThreeSides
* @description Calculate the area of a triangle with the all three sides given.
* @param {Integer} side1 - Integer
* @param {Integer} side2 - Integer
* @param {Integer} side3 - Integer
* @return {Integer} - area of triangle.
* @see [areaTriangleWithAllThreeSides](https://en.wikipedia.org/wiki/Heron%27s_formula)
* @example areaTriangleWithAllThreeSides(5, 6, 7) = 14.7
*/
const areaTriangleWithAllThreeSides = (side1, side2, side3) => {
validateNumericParam(side1, 'side1')
validateNumericParam(side2, 'side2')
validateNumericParam(side3, 'side3')
if (
side1 + side2 <= side3 ||
side1 + side3 <= side2 ||
side2 + side3 <= side1
) {
throw new TypeError('Invalid Triangle sides.')
}
// Finding Semi perimeter of the triangle using formula
const semi = (side1 + side2 + side3) / 2
// Calculating the area of the triangle
const area = Math.sqrt(
semi * (semi - side1) * (semi - side2) * (semi - side3)
)
return Number(area.toFixed(2))
}
/**
* @function areaParallelogram
* @description Calculate the area of a parallelogram.
* @param {Integer} base - Integer
* @param {Integer} height - Integer
* @return {Integer} - base * height
* @see [areaParallelogram](https://en.wikipedia.org/wiki/Area#Dissection,_parallelograms,_and_triangles)View on GitHub (pinned to 5c39e87a9a)
Solutions
- Validate triangle inequality in your own code before calling: assert side1 + side2 > maxSide for each permutation of the three sides.
- Inspect the raw inputs for zero, negative, or NaN values that may have slipped through validateNumericParam upstream.
- If degenerate triangles (collinear points, zero area) are acceptable in your domain, wrap the call in try/catch and treat the failure as area 0.
- Check that the three values correspond to the correct sides and were not transposed or duplicated during data binding.
Example fix
// before
const area = areaTriangleWithAllThreeSides(a, b, c) // throws if invalid
// after
function safeTriangleArea(s1, s2, s3) {
const sides = [s1, s2, s3].sort((x, y) => x - y)
if (sides[0] + sides[1] <= sides[2]) return 0 // degenerate or invalid
return areaTriangleWithAllThreeSides(s1, s2, s3)
} Defensive patterns
Strategy: validation
Validate before calling
function canFormTriangle(s1, s2, s3) {
const sides = [s1, s2, s3]
// all must be positive numbers
if (!sides.every(v => typeof v === 'number' && v > 0)) return false
sides.sort((a, b) => a - b)
return sides[0] + sides[1] > sides[2] // strict: degenerate rejected
}
if (!canFormTriangle(a, b, c)) {
// skip or return 0; do not call areaTriangleWithAllThreeSides
} Type guard
function isValidTriangle(s1, s2, s3) {
return [s1, s2, s3].every(v => typeof v === 'number' && Number.isFinite(v) && v > 0)
&& s1 + s2 > s3 && s1 + s3 > s2 && s2 + s3 > s1
} Try / catch
try {
const area = areaTriangleWithAllThreeSides(a, b, c)
} catch (e) {
if (e instanceof TypeError && /Triangle sides/.test(e.message)) {
// invalid triangle — treat as zero area or surface to caller
} else throw e
} Prevention
- Always sort the three sides and check the two smallest sum strictly greater than the largest before calling.
- Reject zero-length sides in your own validator; the library also rejects them via the inequality.
- Unit-test the boundary case (degenerate triangle) explicitly at your call site.
When it happens
Trigger: Call areaTriangleWithAllThreeSides(1, 2, 10) where 1+2 <= 10; or any call where the largest side is greater than or equal to the sum of the other two (e.g. areaTriangleWithAllThreeSides(5, 5, 10) triggers because 5+5 <= 10). Passing zero-length sides where the inequality collapses (e.g. 0, 0, 5) also triggers it.
Common situations: User input collected from form fields arriving as strings parsed with parseInt but unvalidated for geometric feasibility; CSV/imported data with measurement errors or missing values represented as 0; unit-conversion mistakes (mm vs cm) making one side disproportionately large.
Related errors
- The ${paramName} only accepts non-negative values
- Input is not a valid 2D matrix.
- The two parameters must be distinct, non-null integers
- canvasWidth should be greater than zero
- The arg must be a valid, non empty string
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/8c546d2abbbf1aa6.
Report an issue: GitHub.