I am building a small project of making a simple Calculator using HTML, CSS & Javascript. I am using eval function. I wanted to have both features of Math Error and Syntax Error like a real calculator and I have successfully added Math Error feature but I couldn't add the Syntax Error. So the problem is, If I do this:
var a = "2+2";
alert(eval(a));
It shows the right answer: 4. But If I do this:
var a = "2++2";
alert(eval(a));
document.write(eval(a));
then the eval function is not executed and shows nothing on screen. It shows this on the console.
Uncaught SyntaxError: Invalid left-hand side expression in postfix operation
But I want it to show just a "Syntax Error" on the screen.
Uncaught SyntaxError: Invalid left-hand side expression in postfix operation.SyntaxError: invalid increment/decrement operand)try/catchto handle the error by displaying it