JavaScript(JS) JS calculate the area of a triangle
To calculate the area of a triangle in JavaScript, you can use the formula A = 0.5 * b * h
, where A
is the area of the triangle, b
is the length of the base of the triangle, and h
is the height of the triangle. Here's an example:
let base = 10; let height = 5; let area = 0.5 * base * height; console.log(area); // Output: 25
In the example above, we declare two variables base
and height
with the values 10
and 5
, respectively. We then calculate the area of the triangle using the formula 0.5 * base * height
and store the result in a new variable area
. Finally, we log the value of area
to the console, which outputs 25
.
Note that the variables base
and height
should both be measured in the same units (e.g. centimeters or inches) in order for the formula to work correctly.