
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- Validating a square in a 2-D plane in JavaScript
- How to finddistance between two points in a plane with Cartesian coordinates?
- Finding transpose of a 2-D array JavaScript
- Finding hamming distance in a string in JavaScript
- C program to calculate distance between two points
- Swift Program to Calculate Distance Between Two Points
- Finding letter distance in strings - JavaScript
- Finding shared element between two strings - JavaScript
- Finding the difference between two arrays - JavaScript
- Hamming Distance between two strings in JavaScript
- Finding the sum of minimum value in each row of a 2-D array using JavaScript
- The distance between the points \( A(0,6) \) and \( B(0,-2) \) is(A) 6(B) 8(C) 4(D) 2
- Finding points nearest to origin in JavaScript
- Program to Find the Shortest Distance Between Two Points in C++
- Finding longest substring between two same characters JavaScript

Advertisements