- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Finding distance between two points in a 2-D plane using JavaScript

## Problem

We are required to write a JavaScript function that takes in two objects both having x and y property specifying two points in a plane.

Our function should find and return the distance between those two points.

## Example

Following is the code −

const a = {x: 5, y: -4}; const b = {x: 8, y: 12}; const distanceBetweenPoints = (a = {}, b = {}) => { let distance = 0; let x1 = a.x, x2 = b.x, y1 = a.y, y2 = b.y; distance = Math.sqrt((x2 - x1) * 2 + (y2 - y1) * 2); return distance; }; console.log(distanceBetweenPoints(a, b));

## Output

6.164414002968976

- Related Questions & Answers
- Validating a square in a 2-D plane in JavaScript
- Finding transpose of a 2-D array JavaScript
- C program to calculate distance between two points
- Find mirror image of a point in 2-D plane in C++
- Equality of two 2-D arrays - JavaScript
- C++ Program to check whether points in a 3-D plane are Coplanar
- Sorting 2-D array of strings and finding the diagonal element using JavaScript
- Finding the sum of minimum value in each row of a 2-D array using JavaScript
- Hamming Distance between two strings in JavaScript
- Distance between 2 duplicate numbers in an array JavaScript
- Program to Find the Shortest Distance Between Two Points in C++
- Evaluate a 2-D polynomial at points (x, y) in Python
- Finding hamming distance in a string in JavaScript
- Finding letter distance in strings - JavaScript
- Reshaping 2-D array in JavaScript

Advertisements