/    Sign up×
Community /Pin to ProfileBookmark

typecasting in JS ?

How do you typecast in JS ?

I have :

var x = document.screen2.cust.value;

which comes as cust=”3″ and I need to do aritmatic + or – on this value.

document.screen2.cust.value +1 give 31 instead of 4….it is appending the string “3” instead of adding 1 to integer value 3.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@fredmvJan 15.2004 — JavaScript doesn't support genuine typecasting, however, it has methods in which allow you to convert variables to different data types. In this example, if you want the value of the form element to be treated as a number, you have quite a few options, one being the [font=courier]Number[/font] constructor:var x = Number(document.screen2.cust.value);
×

Success!

Help @RJ2003 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...