/    Sign up×
Community /Pin to ProfileBookmark

how a variable’s type can be changed on-the-fly.

Hello,

I am a newbie of javascript.
I am not clear about the definition which is as follows.
A JavaScript that shows how a variable’s type can be changed on-the-fly.

Does this program means to :
Strings can be converted to numbers
– var i = parseInt(“37 hello”);
– var d = parseFloat(“6.02 hello”);

If anyother code is required then do help me out..
Thanks in advance..

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorFeb 21.2011 — Well, strictly speaking, JavaScript has no typically methods to change the type of the variables, but it can parse/stringify some variables if their structure permits that. In some cases, when change it, it will return paradoxically typeof values ?

[B]parseInt()[/B] and [B]parseFloat()[/B] are, in fact parsing methods.

Let's take parseIn(). The full syntax is [B]parseInt([/B][I]value[/I],[I]base[/I][B])[/B], and it was design to parse an integer number from a [I]base to decimal[/I]

<i>
</i>var decimalInt=parseInt('ff',16)
alert(decimalInt) // returns 255, the decimal equivalent of the hexa number "FF"

Collaterally, it can be used to extract the decimal integer from a construction like "INT+STRING" like "25px". Or to extract the integer from a floated decimal like "INT.FLOAT" like "25.47". But this is not the main purpose of the method.

There is also the [B]Number([/B][I]value[/I][B])[/B] method, which can transform a number written like a string into a decimal number.

But, still, if the variable has the value "xyz", [B]parseInt()[/B] will return a NaN (Not A Number). That is a paradoxically property, as the type pf the variable is a number, indeed, but is a number which has the property NaN, that means a number which is not a number ? Sounds silly, but this is how it works.

You may also change an array into a string, using the array's method join()
<i>
</i>var stringArray=(['a','b','c'].join())
alert(typeof(stringArray)) // returns a String type of


Or, you may parse a decimal integer into a number of another base (which has the type of = String) using[B] toString([/B][I]base[/I][B])[/B]
<i>
</i>var numberHexa=(255).toString(16);
alert(numberHexa); // returns "ff", the hexa equivaluent of the decimal 255
alert(typeof(numberHexa)) // returns String


There are also methods to transform a String into a JSON Object, but only if the string is written following a certain JSON syntax.

Well, you may consider those methods to be able to change the type of a variable, but there are a lot of restrictive conditions which those variables have to comply before being "transformed". In fact those methods transform those variables into [I]something else[/I]. The new type is a[I] result[/I] of that transformation, not a [I]scope[/I] in itself.
×

Success!

Help @miaaa00 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.17,
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,
)...