Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Pages - Customize & Extend
Answered

Adding two integers together - surely it's not that hard!

(0) ShareShare
ReportReport
Posted on by 7

I need to add two integers together in JavaScript - no matter what I do, I just can't seem to get anything to work. I started with

var varC = $('#varA').val() + $('#varB').val();

This just concatenated them. I then found out that adding .val() returns a string so that would explain it.I tried removing the '.val()' but it didn't return anything. I've tried replacing the 'var' with 'let' but, again, no joy.

I've tried looking for some sort of function that would return what I want but I can't find anything. Can anyone please tell me how I can do this?

 
 
Categories:
  • JonEvans Profile Picture
    7 on at
    Re: Adding two integers together - surely it's not that hard!

    That's worked brilliantly, thank you.

  • Verified answer
    Fubar Profile Picture
    7,960 Super User 2025 Season 1 on at
    Re: Adding two integers together - surely it's not that hard!

    If its returning a string you need to convert it to a number in JavaScript 

    var x = Number("1") + Number("2");

    Note: defining variables via "let" just affects the variable scope not its type.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Michael Gernaey – Community Spotlight

We are honored to recognize Michael Gernaey as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Featured topics