
- 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 area of triangle in JavaScript using Heron’s formula
We are given the lengths of three sides of a triangle and we are required to write a function that returns the area of the triangle using the length of its sides.
Heron's formula
We can calculate the area of a triangle if we know the lengths of all three sides, using Heron's formula −
Step 1 − Calculate "s" (half of the triangles perimeter) −
s = (a+b+c) / 2
Step 2 − Then calculate the Area using Herons formula −
A = sqrt( s(s-a)(s-b)(s-c) )
Example
So, Let’s write the code for this function −
const sides = [12, 4, 9]; const areaOfTriangle = sides => { const [a, b, c] = sides; const sp = (a + b + c) / 2; const aDifference = sp - a; const bDiffernece = sp - b; const cDifference = sp - c; const area = Math.sqrt(sp * aDifference * bDiffernece * cDifference); return area; }; console.log(areaOfTriangle(sides));
Output
The output in the console: −
13.635890143294644
- Related Articles
- Java Program to calculate the area of a triangle using Heron's Formula
- Explain Heron's formula.
- What is Heron's formula?
- Finding the elements of nth row of Pascal's triangle in JavaScript
- A traffic signal board, indicating 'SCHOOL AHEAD', is an equilateral triangle with side 'a'. Find the area of the signal board using Heron's formula. If its perimeter is 180 cm, what will be the area of the signal board?
- Give the formula for the area of a Triangle.
- Derive the formula for finding the base of a Right angle triangle.
- Finding middlemost element(s) in JavaScript
- Finding the most frequent word(s) in an array using JavaScript
- Calculating the area of a triangle using its three sides in JavaScript
- Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript
- Finding length, width and height of the sheet required to cover some area using JavaScript
- Finding pandigital numbers using JavaScript
- Finding product of an array using recursion in JavaScript
- Finding smallest number using recursion in JavaScript

Advertisements