javascript - How to take plus sign in variable - Stack Overflow

I want to calculate two numbers and its pretty simple.But Is there any way to take operator in variable

I want to calculate two numbers and its pretty simple.

But Is there any way to take operator in variable and then do the calculation?

var x = 5;
var y = 5;
var p = '+';
var z = x + p + y;

$(".button").click(function() {
  alert(z);
});
<script src=".1.1/jquery.min.js"></script>
<div class="button">Click ME !</div>

I want to calculate two numbers and its pretty simple.

But Is there any way to take operator in variable and then do the calculation?

var x = 5;
var y = 5;
var p = '+';
var z = x + p + y;

$(".button").click(function() {
  alert(z);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="button">Click ME !</div>

Share Improve this question edited Jul 4, 2015 at 18:23 Salman Arshad 273k84 gold badges443 silver badges534 bronze badges asked Apr 14, 2015 at 8:08 TwixTwix 3874 silver badges16 bronze badges 3
  • 2 A simple switch...case would do. – Salman Arshad Commented Apr 14, 2015 at 8:11
  • Here's answer to your question - stackoverflow./questions/5834318/… – Yellen Commented Apr 14, 2015 at 8:12
  • 1 If you only need to decide between + and - you can also just multiply with var p = 1 or var p = -1. – Nils_M Commented Apr 14, 2015 at 10:05
Add a ment  | 

4 Answers 4

Reset to default 15

Avoid eval whenever possible. For this example, a simple switch...case statement will be sufficient:

var x = 5;
var y = 5;
var z;
var p = "+";
switch (p) {
    case "+":
        z = x + y;
        break;
    case "-":
        z = x - y;
        break;
}

You can also use a map of functions:

var fnlist = {
    "+": function(a, b) { return a + b; },
    "-": function(a, b) { return a - b; }
}
var x = 5;
var y = 5;
var p = "+";
var z = fnlist[p](x, y);

Or use parseInt on the string which you will be adding the variable to:

	var x = 5;
	var y = 5;
	var p = '-';
	var z = x + parseInt(p + y);

$(".button").click(function(){
  alert(z);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="button">Click ME !</div>

You are looking for eval function:

	var x = 5;
	var y = 5;
	var p = '+';
	var z = x + p + y;

$(".button").click(function(){
  alert(eval(z));
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="button">Click ME !</div>

However, you have to remember that using eval function is potentially risky. For example, if used in the wrong way it can allow one to make injection attacks. Debugging can be also more difficult. I suggest to read this question.

you can use the eval function:

var x = 5;
var y = 5;
var p = '+';
var z = eval(x + p + y);

alert(z);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743573670a4473252.html

相关推荐

  • javascript - How to take plus sign in variable - Stack Overflow

    I want to calculate two numbers and its pretty simple.But Is there any way to take operator in variable

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信