# How to Find the Distance Between Two Points in JavaScript

Contents

In this article, you will learn how to find the distance between two points in JavaScript.

## Finding the distance between two points in JavaScript

To find the distance between two points in JavaScript, you can use the distance formula:

`distance = √((x2 - x1)² + (y2 - y1)²)`

Where (x1, y1) and (x2, y2) are the coordinates of the two points.

### Examples

Here is an example of how to use this formula in JavaScript:

```
const x1 = 1;
const y1 = 2;
const x2 = 4;
const y2 = 6;
const distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
console.log(distance); // Output: 5
```

In this example, we have two points with coordinates (1, 2) and (4, 6). We use the distance formula to calculate the distance between these two points, which is 5 units.

We start by defining the coordinates of the two points, and then use the Math.pow method to calculate the squares of the differences between the x and y coordinates. We then add these two squares, take the square root using the Math.sqrt method, and assign the result to the distance variable.

We can then log the value of distance to the console to verify that it is correct.

You can use this formula and code to find the distance between any two points in a 2D plane.