/    Sign up×
Community /Pin to ProfileBookmark

Declaring variables

var i : Integer;

Does not work in my mozilla firefox 1.0.6 browser.

Also,

function add(var1,var2)
{
document.write(var1+var2);
}

simply concatenates the parameters passed.

Please help

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@BigMoosieSep 12.2005 — var i [B]=[/B] Integer;

you values must be strings, convert them to floats first:

var1 = parseFloat(var1)||0; // for floats

var2 = parseInt(var2)||0; // for integers
Copy linkTweet thisAlerts:
@Jeff_MottSep 12.2005 — var1 = parseFloat(var1)||0; // for floats

var2 = parseInt(var2)||0; // for integers[/quote]
Or the [font=courier new]Number()[/font] function, which handles numbers in any known format, and is more intelligent about what it returns.

[font=courier new]var1 = Number(var1);

var2 = Number(var2);[/font]
Copy linkTweet thisAlerts:
@BigMoosieSep 12.2005 — How is it more intelligent? I would say parseFloat is more intelligent as it will turn "100cm" into 100.

Ok, I guess you were referring to e notion?
Copy linkTweet thisAlerts:
@dayanandasekarauthorSep 12.2005 — Hey Big Mossie,

The syntax

var i=Integer;

isn't working either.

I follow a book which says

var cla : Integer;

is also not working.

Please help Big Mossie.
Copy linkTweet thisAlerts:
@Jeff_MottSep 12.2005 — I would say parseFloat is more intelligent as it will turn "100cm" into 100.[/quote]Despite that side effect parseFloat is still completely ignorant to what the suffix that it leaves off means. For instance, parseFloat("100cm") + parseFloat("100mm") will not give you the answer you're looking for. And parseFloat("100 blah blah blah") is certainly not going to be the number of units of measure you're looking for. It's not intelligent, it just happens to be a convienence in very special cases when you know what the input is (note that knowing what input [b]is[/b] is not the same as knowing what input [b]should be[/b]).

Another example, what if I asked you personally to give me the sum of "1Q$0!" and "5L^93~". Would you tell me the answer is 6? Or would you say to me: these aren't numbers; I can't add them? parseFloat would tell you the answer is 6, whereas Number would tell you these are not numbers.

Using the Number function ensures that your input is indeed a number and not some goop that we pick digits out of.
Copy linkTweet thisAlerts:
@felgallSep 12.2005 — var cla : Integer;

This is only valid in Javascript 2.0 and no browser supports that yet. It may also valid is JScript when running as part of ASP on the server (but I am not sure about that).

Javascript 1.5 doesn't allow you to define the type of variables, it is derived from whatever is assigned to it.
×

Success!

Help @dayanandasekar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...