Dev Helpers
← Back

Calculate polygon area by shoelace

Calculate polygon area by shoelace using JavaScript.

Open this example in the interactive library

Source code

const points = [
  [0, 0],
  [4, 0],
  [4, 3],
  [0, 3],
];
console.log(
  Math.abs(
    points.reduce((sum, [x, y], i) => {
      const [a, b] = points[(i + 1) % points.length];
      return sum + x * b - y * a;
    }, 0),
  ) / 2,
);

Sample output

12