Skip to content
Related Articles

Related Articles

How to find the area of a triangle using JavaScript ?

Improve Article
Save Article
  • Last Updated : 29 Jan, 2021
Improve Article
Save Article

Given an HTML document containing input fields that holds the side of triangle i.e. side1, side2, and side 3. The task is to find the area of triangle using JavaScript.

Approach: First we will create three input fields using <input type=”number”> tag to holds number input. After filling the input value, when user click the button, then the JavaScript function Area() will be called. 

In JavaScript function, we use document.getElementById(“side1”).value to get the input value and then apply parseInt() method on it to get the input value in number. And then use simple mathematical formula to find the area of triangle and use document.getElementById(“display”).innerHTML to display the output on the screen.

Formula to find the Area of Triangle:

var s = (side1 + side2 + side3) / 2;

var area = Math.sqrt(s * ((s - side1) * (s - side2) * (s - side3)));



    <meta http-equiv="Content-Type" 
        content="text/html; charset=utf-8">
        JavaScript function to find 
        the area of a triangle
<body style="text-align: center;">
    <h1 style="color: green;">
        JavaScript function to find 
        the area of a triangle
    <label for="side1">
        Enter the value of side 1: 
    <input type="number" id="side1" 
        placeholder="Enter value of side 1">
    <label for="side2">
        Enter the value of side 2: 
    <input type="number" id="side2" 
        placeholder="Enter value of side 2">
    <label for="side3">
        Enter the value of side 3: 
    <input type="number" id="side3" 
        placeholder="Enter value of side 2">
    <button onclick="Area()">Click Here!</button>
        Area of Triangle: <span id="display"></span>
    <script type="text/javascript">
        function Area() {
            var side1 = parseInt(document
            var side2 = parseInt(document
            var side3 = parseInt(document
            var s = (side1 + side2 + side3) / 2;
            var area = Math.sqrt(s * ((s - side1) 
                    * (s - side2) * (s - side3)));
                "display").innerHTML = area;


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!