0

ans = 2 + 3;
console.log(`ans`, ans); // 5 

I know this will give the sum of 2 + 3 which is 5 but is there a way I can pass the arithmetic operation(+,-,*,/) in a string?

Eg:

let operation =  '+'; // it can be +, -, *, / 

let ans = 2 operation 3; // how I can do this
console.log(`ans`, ans); // 5 
  • 1
    Hi, you may need to use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval or use if - else - switch statements to determine the type of the operation. – ihpar Sep 05 '22 at 07:11

0 Answers0