JavaScript Program To Calculate The Area Of A Triangle - CodingBroz
Maybe your like
In this post, we will learn how to calculate the area of a triangle using JavaScript.

The area of a triangle is defined as the total space occupied by the three sides of a triangle in a 2-dimensional plane.
The area of a triangle can be calculated using various methods. In this post, we will be calculating the area using two different approaches.
These two methods are as follows:-
- Using Base and Height of the Triangle
- Using the Sides of the Triangle
So, without further ado, let’s begin this tutorial.
Table of Contents- JavaScript Program To Calculate The Area of a Triangle
- How Does This Program Work ?
- JavaScript Program To Calculate Area of Triangle When All Sides are Known
- Conclusion
- Related
JavaScript Program To Calculate The Area of a Triangle
//JavaScript Program To Calculate The Area of a Triangle var base = parseInt(prompt("Enter the base: ")); var height = parseInt(prompt("Enter the height: ")); //Calculating the area var area = (base * height) / 2; //Display Output console.log("Base: " + base); console.log("Height: " + height); console.log("The area of the triangle is " + area);Output
Base: 3 Height: 4 The area of the triangle is 6How Does This Program Work ?
var base = parseInt(prompt("Enter the base: ")); var height = parseInt(prompt("Enter the height: "));The user is asked to enter the value of base and height of the triangle.
//Calculating the area var area = (base * height) / 2;Then, we calculate the area of the triangle using ½ x Base x Height.
//Display Output console.log("Base: " + base); console.log("Height: " + height); console.log("The area of the triangle is " + area);Finally, the area of the triangle is printed on the screen using console.log() function.
JavaScript Program To Calculate Area of Triangle When All Sides are Known
//JavaScript Program To Calculate The Area of a Triangle var first_side = parseInt(prompt("Enter first side: ")); var second_side = parseInt(prompt("Enter second side: ")); var third_side = parseInt(prompt("Enter third side: ")); //Calculating semi-perimeter var s = (first_side + second_side + third_side) / 2 //Calculating Area var area = Math.sqrt(s * (s - first_side) * (s - second_side) * (s - third_side)); console.log("Area: " + area);Output
Enter first side: 3 Enter second side: 4 Enter third side: 5 Area: 6 //Calculating semi-perimeter var s = (first_side + second_side + third_side) / 2We calculate the semi-perimeter of the triangle using s = (a + b + c) / 2.
var area = Math.sqrt(s * (s - first_side) * (s - second_side) * (s - third_side));Then, we calculate the area of the triangle using the formula:
Area = [s((s-a)(s-b)(s-c))]**½
The Math.sqrt() method is used to calculate the square root of the number.
console.log("Area: " + area);Finally, the area of the triangle is printed on the screen using the console.log() method.
Conclusion
I hope after going through this post, you understand how to calculate the area of a triangle using JavaScript.
If you have any query regarding the program, feel free to contact us in the comment section. We will be delighted to help you.
Also Read:
- JavaScript Hello World Program
- JavaScript Program To Add Two Numbers
- JavaScript Program To Find The Square Root
- JavaScript Program To Find Average of N Numbers
Related
Tag » Area Of Triangle Javascript Code
-
How To Find The Area Of A Triangle Using JavaScript ? - GeeksforGeeks
-
JavaScript Basic: Find The Area Of A Triangle Where Lengths Of The ...
-
JavaScript Program To Calculate The Area Of A Triangle - Programiz
-
Javascript Program To Calculate The Area Of A Triangle Using All Formulas
-
Calculate The Area Of A Triangle In JavaScript
-
JS Function Calculate Triangle Area With 3 Sides - CodePen
-
Calculate The Area Of Triangle Using JavaScript Code - YouTube
-
JavaScript Code To Find Area Of Triangle AnWriter Mobile - YouTube
-
Calculating The Area Of Triangle Using Javascript - Gists · GitHub
-
Q: Calculate The Area Of Triangle By Its 3 Sides In JavaScript
-
How To Get Area Of The Triangle Using JavaScript
-
Calculate Area Of A Triangle Js Code Example - Code Grepper
-
Area Of Triangle In Javascript Code Example - Code Grepper
-
Javascript Program To Find Area Of Triangle Using Base And Height ...