...

View Full Version : Possible to define variable as integer?



dazappa
12-16-2005, 06:59 PM
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.

PhotoJoe47
12-16-2005, 07:56 PM
You can use the parseInt() function to make sure the variable is view as a interger.

For example:



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

dazappa
12-16-2005, 08:37 PM
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.

felgall
12-16-2005, 09:09 PM
- 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.

dazappa
12-16-2005, 09:17 PM
- 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:D thanks again.

Pyth007
12-19-2005, 12:49 PM
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:

Is it possible to define a variable as an integer in javascript
The literal answer is NO! :eek:
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:

var myNumber = "15";
alert("First, myNumber is a "+typeof myNumber+"...\nNow it is a "+typeof parseInt(myNumber)+"!");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum