35
var a = 1;
var b = 2;
var c = a+b;

c will show as 12; but I need 3

How do I do it using jQuery?

1
  • 4
    Not sure if serious :/ Commented Jul 26, 2020 at 15:06

6 Answers 6

84

Definitely use jQuery

Because the power of jQuery is obviously unparalleled, here is how to do with with 100% jQuery:

var a = 1;
var b = 2;
$("<script>c=+("+a+")+ +("+b+")</script>").appendTo($(document));

Now c will hold your result and you used nothing but jQuery! As you can see jQuery is really great because it does all sorts of things

This also works well because it doesn't matter if a or b are strings!


Not enough jQuery?

var a = 1;
var b = 2;
$("<script id='test'>$('<textarea id=\\'abc\\'>'+("+a+")+ +("+b+")+'</textarea>').appendTo($('body'))</script>").appendTo('body');
var c = $("#abc").val();

This answer was done with 100% jQuery because jQuery is awesome but only use this carefully because it might not work sometimes.


jQuery Arithmetic Plugin

You can also use the revolutionary jQuery Arithmetic Plugin this has solved world peace in over 4294967295 (>> 0 === -1) countries:

var a = 1;
var b = 2;
var c = $.add(a,b);

while this is all great (this is not confirmed), but in jQuery -3.0.1 I have heard you will be able to add numbers this way:

$.number($.one, $.two).add($.number($.three, $.four))

this adds 12 (one + two) to 34 (three + four)

Sign up to request clarification or add additional context in comments.

1 Comment

This is awesome and hilarious.
39

It looks like you have strings and not numbers, you need parseInt() or parseFloat() (if they may be decimals) here, like this:

var a = "1";
var b = "2";
var c = parseInt(a, 10) + parseInt(b, 10);
//or: var c = parseFloat(a) + parseFloat(b);

You can test the difference here, it's worth noting these are not jQuery but base JavaScript functions, so this isn't dependent on the jQuery library in any way.

3 Comments

I had to downvote this, because you should never use parseInt to cast strings to number. Rather, use Number().
Maybe, just maybe, because of implications like discussed at stackoverflow.com/questions/4090518/…
Not enough jQuery.
11

Just try this:

var a = 1;
var b = 2;
var c = (+a) + (+b);
alert(c); //or whatever you want

Comments

10

Try this -

var c = parseInt(a, 10) + parseInt(b, 10);

2 Comments

You should always use a radix on parseInt(), otherwise numbers starting with a 0 will be treated as base 16 by default.
Watch out with this one, it will use "0" as a magic prefix for octal, so: parseInt("010") == 8.
2

This performs the addition of two variables supplied from input variables selected on the basis of ID.

var salary, tds, netSalary;
salary = parseInt($("#txtSalary").val());
$("#txtTds").on('mouseenter focus', function ()
{
    tds = parseInt($("#txtSalary").val() * (0.1));
    $("#txtTds").val(tds);
});
$("#txtNetSalary").on('mouseenter focus', function () {
    netSalary = parseInt($("#txtSalary").val() +("#txtTds").val());
    $("#txtNetSalary").val(netSalary);
});

Comments

1
    var a =10;
    var b=10;
    var c=a+b;
    alert(c);

    //in  double case
     var e=10.5;
     var f=10.5;
     alert(e+f);

     //forcelly convert to int
     alert(parseInt(e)+parseInt(f));

     //forcelly convert to float
     alert(parseFloat(a)+parseFloat(b));


Example   :

 https://jsfiddle.net/v0rjek67/6/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.