Calculate the Mean Value in JavaScript

The mean value represents the average of a dataset or an array. It can be calculated using the average from either sorted or unsorted arrays, as well as hash tables. To determine the mean, we sum all the values and then divide by the total number of elements in the dataset.

Syntax

The syntax provided below is utilized to determine the "mean" value in JavaScript as well as in various other programming languages.

Example

Mean value = total sum of the array values/ total array elements

To determine the Mean:

  • Initially, sum up all the values present in the array.
  • Then, simply divide the resulting total by the number of elements in the array.
  • Examples

The subsequent examples illustrate the mean or average value derived from both the sorted and unsorted arrays, in addition to the hash value.

Example 1:

The subsequent illustrations demonstrate the average, or mean, value derived from the ordered array.

Example

<!DOCTYPE html>
<html>
<head>
<title> Calculate Mean value in Javascript </title>
</head>
<body style = "background-color:beige;">
<h2> Calculate Mean value in Javascript </h2>
<h4>  javascript calculate the mean value of sorted array </h4>
<p> The mean value is the average of the array and group of the data. </p>
<b id = "data_view"> </b>
<script>
//Function for calculating mean value
function findMeanValue(a, sum)
{
//Find Mean of an array
let sum_value = 0;
for (let i = 0; i < num; i++)
sum_value += a[i];
return sum_value / num;
}
let a = [0, 10, 2, 3, 4, 5, 6, 7]
let num = a.length;
// Function call and get output
document.write("Mean = " + findMeanValue(a, num) + "<br>");
</script>
</body>
</html>

Output

The output shows the mean values using Function.

Example 2:

The subsequent examples illustrate the average, or mean, value of the array that has not been sorted.

Example

<!DOCTYPE html>
<html>
<head>
<title> Calculate Mean value in Javascript </title>
</head>
<body style = "background-color:beige;">
<h2> Calculate Mean value in Javascript </h2>
<h4>  javascript calculate the mean value of unsorted array </h4>
<p> The mean value is the average of the array and group of the data. </p>
<b id = "data_view"> </b>
<script>
//Function for calculating mean value
function findMeanValue(a, sum)
{
//Find Mean of an array
let sum_value = 0;
for (let i = 0; i < num; i++)
sum_value += a[i];

return sum_value / num;
}
let a = [1, 9, 4, 2, 6, 5, 8, 7]
let num = a.length;
// Function call and get output
document.write("Mean = " + findMeanValue(a, num) + "<br>");
</script>
</body>
</html>

Output

The output shows the mean values using Function.

Example 3:

The subsequent illustrations demonstrate the mean or average value derived from the organized hash.

Example

<!DOCTYPE html>
<html>
<head>
<title> Calculate Mean value in Javascript </title>
</head>
<body style = "background-color:beige;">
<h2> Calculate Mean value in Javascript </h2>
<h4>  javascript calculates the mean value of sorted hash value </h4>
<p> The mean value is the average of the array, hash, and group of the data. </p>
<script>
let a ={
1: 100,
2: 200,
3: 300,
4: 400,
}
let sum = 0;
//Function for calculating mean value
function findMeanValue(a, sum)
{
//Find Mean of a hash
let sum_value = 0;
for (const key in a) {
sum_value += a[key];
}
return sum_value / num;
}
let num = Object.keys(a).length;
// Function call and get output
document.write("Mean = " + findMeanValue(a, num) + "<br>");
</script>
</body>
</html>

Output

The output shows the mean values using Function.

Example 4:

The subsequent examples illustrate the average or mean value of the hash that has not been sorted.

Example

<!DOCTYPE html>
<html>
<head>
<title> Calculate Mean value in Javascript </title>
</head>
<body style = "background-color:beige;">
<h2> Calculate Mean value in Javascript </h2>
<h4>  javascript calculates the mean value of unsorted hash value </h4>
<p> The mean value is the average of the array, hash, and group of the data. </p>
<script>
let a ={
1: 240,
2: 80,
3: 500,
4: 400,
}
let sum = 0;
//Function for calculating mean value
function findMeanValue(a, sum)
{
//Find Mean of a hash
let sum_value = 0;
for (const key in a) {
sum_value += a[key];
}

return sum_value / num;
}
let num = Object.keys(a).length;
// Function call and get output
document.write("Mean = " + findMeanValue(a, num) + "<br>");
</script>
</body>
</html>

Output

The output shows the mean values using Function .

Conclusion

The mean value represents the average of a collection, such as an array or a set organized like a hash.

Input Required

This code uses input(). Please provide values below: