skip to Main Content
alert('The result is ${eval('${num1} ${operator} ${num2}')}'); 

eval() function used to perform operation on two numbers (num1 and num2 are numbers and operator is "+ , – , * , /") but I got problem in the code. is there any mistake in my code?

2

Answers


  1. Try:

    alert(`The result is ${eval(`${num1} ${operator} ${num2}`)}`); 
    

    Backticks instead of single quotes when using template literals.
    That said, I would caution against using eval(), as it can execute arbitrary code and might pose security risks if used with user input.

    Login or Signup to reply.
  2. Slightly tilt your single quotes and voila!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search