I was making a calculator which was served as a static HTML page to the user's browser. The page is not designed to submit any information back to the server. Nothing else will be present on the web page except for this calculator.
Is it safe to use "eval" in this scenario? Or put another way, does the use of eval in this case cause additional security risk?
To me, it doesn't seem like the user can do anything nefarious with this page they can't do by simply opening the browsers development tools. I have always read "never" use eval, but in this case, it seems like it makes good sense.
Here is an example calculator:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
</head>
<body>
<input id="input" type="text">
<button onclick="{
let result= '';
try {
result = eval(document.getElementById('input').value);
} catch (error) {
result = error;
}
console.log(result);
document.getElementById('result').innerHTML=result;
}">calculate</button>
<div id="result">result here</div>
</body>
</html>