Euclidean distance

JavaScript, Math, Algorithm · Dec 28, 2020

Calculates the distance between two points in any number of dimensions.

const euclideanDistance = (a, b) =>
  Math.hypot(...Object.keys(a).map(k => b[k] - a[k]));
euclideanDistance([1, 1], [2, 3]); // ~2.2361
euclideanDistance([1, 1, 1], [2, 3, 2]); // ~2.4495

Written by Angelos Chalaris

I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.

If you want to keep in touch, follow me on GitHub.

More like this

  • JavaScript Algorithms

    Learn a handful of popular algorithms, implemented in JavaScript ES6.

    Collection · 35 snippets

  • Distance between two points

    Calculates the distance between two points.

    JavaScript, Math · Dec 28, 2020

  • Vector distance

    Calculates the distance between two vectors.

    JavaScript, Math · Dec 28, 2020

  • Hamming distance

    Calculates the Hamming distance between two values.

    JavaScript, Math · Dec 28, 2020