The grouping operator is used for managing precedence of expressions evaluation.
Following is the code for grouping operator in JavaScript −
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.sample,.result {
font-size: 18px;
font-weight: 500;
color: red;
}
.result {
color: rebeccapurple;
}
</style>
</head>
<body>
<h1>Grouping operator in JavaScript</h1>
<div class="sample">2+2*5/22</div>
<div class="result"></div>
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to use group operator to specify precedence</h3>
<script>
let sampleEle = document.querySelector(".sample");
let resultEle = document.querySelector(".result");
document.querySelector(".Btn").addEventListener("click", () => {
resultEle.innerHTML += "2+2*5/22 = " + (2 + (2 * 5) / 22) + "<br>";
resultEle.innerHTML += "(2+2)*5/22 = " + ((2 + 2) * 5) / 22 + "<br>";
resultEle.innerHTML += "(2+2*5)/22 = " + (2 + 2 * 5) / 22 + "<br>";
});
</script>
</body>
</html>Output
The above code will produce the following output −

On clicking the ‘CLICK HERE’ button −
