I want to store "+" operator in variable.
<head>
<script type="text/javascript">
var m= function(a,b){
return a-b
}
var jj= 10 m 10;
alert(jj)
</script>
</head>
Avoiding the use of eval
, I would recommend to use a map of functions :
var operators = {
'+': function(a, b){ return a+b},
'-': function(a, b){ return a-b}
}
Then you can use
var key = '+';
var c = operators[key](3, 5);
Note that you could also store operators[key]
in a variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With