Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Possible to define variable as integer?

    First, Is it possible to define a variable as an integer in javascript, and second if it is could someone tell me how? In the script I'm working on, I'm trying to add 5 to 40. It comes out 405 because for some reason it thinks that I'm adding a string. So I think to prevent that and make it work I will define it as an integer.

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can use the parseInt() function to make sure the variable is view as a interger.

    For example:

    Code:
     
    val result = 0;
    val x = 40;
    val y = 5;
     
    result = parseInt(x) + parseInt(y);
    But most of the time you don't have to do this. Could I see the code you have written? You may just have a syntax problem.

    Here is an online reference for the function.

    http://www.w3schools.com/jsref/jsref_parseInt.asp

  • #3
    New Coder
    Join Date
    Dec 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok I'll try that webpage, and I'll put up the code in a bit. It's a bit messy though...But here's the wierd thing, I had made (in another part of my script) that could subtract just fine.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    - is only used for subtraction but + has two different meanigs depending on whether the values are numbers (addition) or strings (concatenation). If either value is a string then the other is converted to a string.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New Coder
    Join Date
    Dec 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall
    - is only used for subtraction but + has two different meanigs depending on whether the values are numbers (addition) or strings (concatenation). If either value is a string then the other is converted to a string.
    I knew it was concatenation, i just didn't know how to fix it(to the way i wanted it to be). PhotoJoe thank you so much, your example helped me figure out where to place it and it worked. By the way I don't think anyone cares, but I am making a little javascript game and that just saved my project thanks again.

  • #6
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem that you probably had run into is that the value of elements (eg the value attribute of <input type="text" name="myTextbox"> element) is always set to be a string unless otherwise converted in the js code. Same is true for any other elements, like the value of <option>s in a <select> drop-down, the value of radio buttons, etc.

    To convert to numbers, you can use the parseInt() or parseFloat() methods, using the Number() constructor/function, or as a shortcut (which you accidentally noticed), use some other arithmetic operation before addition (eg myValue-0, myValue*1, or myValue/1 would all convert myValue to a number without changing its value. Subracting-by-zero also has an added benefit of setting myValue to 0 if the element was left blank.

    Oh and an FYI about your first question:
    Quote Originally Posted by dazappa
    Is it possible to define a variable as an integer in javascript
    The literal answer is NO!
    The reason is that js only has a few datatypes that it defines: String, Boolean, Object, Function, Undefined, Null, and Number. Thus the methods mentioned above just converts your String to a Number; parseInt() just states, as an additional effect, what should be done about the decimal values. The typeof operator lets you see what the data-type is for a variable:
    Code:
    var myNumber = "15";
    alert("First, myNumber is a "+typeof myNumber+"...\nNow it is a "+typeof parseInt(myNumber)+"!");
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •