[OFF TOPIC]
quote:
Originally posted by Plan-1130
Comparing the string 10 with the number 10 will result in NaN...
No...
Comparing it using the
Equality Operators (== or !=) or
Relational Operators will result in a match.
Comparing it with the
Identity Operator (=== or !==) will result in a non-match (thus not an NaN-error). You will only get the NaN-error in specific calculations.
PS: It is preferred to use the
Identity Operator (===) as this notifies you of possible type casting problems. It is also faster because JScript doesn't need to convert all variables to the same type, which it otherwise will always do.
See
JScript 5.6 Documentation.
quote:
The following describes how the different groups of operators behave depending on the types and values of expression1 and expression2:
Relational (<, >, <=, >=)
* Attempt to convert both expression1 and expression2 into numbers.
* If both expressions are strings, do a lexicographical string comparison.
* If either expression is NaN, return false.
* Negative zero equals Positive zero.
* Negative Infinity is less than everything including itself.
* Positive Infinity is greater than everything including itself.
Equality (==, !=)
* If the types of the two expressions are different, attempt to convert them to string, number, or Boolean.
* NaN is not equal to anything including itself.
* Negative zero equals positive zero.
* null equals both null and undefined.
* Values are considered equal if they are identical strings, numerically equivalent numbers, the same object, identical Boolean values, or (if different types) they can be coerced into one of these situations.
* Every other comparison is considered unequal.
Identity (===, !==)
These operators behave identically to the equality operators except no type conversion is done, and the types must be the same to be considered equal.
quote:
Originally posted by Plan-1130
Still I don't know how I got many of my scripts working, as VB provides a function to parse an integer from a string
JScript too:
parseInt(numString, [radix]).
eg: var numTen = parseInt("10");
Also see "
CookieRevised's reply to [I help them] VB2JS" for some different uses of that method.
And
parseFloat(numString) is the equivalent of the
Val() function used on a string in VB.
But also note that in many cases you don't need to use it at all especially because of JScript's automatical type conversion.
If you have a string (eg: "10") and you multiply this with the number 5, JScript will automatically convert the string "10" to number 10, you will not get an error!.
Compare this behaviour with Variants in Visual Basic.
That is why this method works (but also why you almost never need to do this either):
quote:
Originally posted by Eljay
Just do varName * 1 to convert it, maybe slightly dodgy but short and simple
quote:
Originally posted by markee
For me I just do th following and it works (though I'm probably going about it the wrong way)code:
var String = "10";
var Number = new Number(String);
I couldn't think of any other way of making it a number. I hope this helps (assuming what I did is correct).
It is a correct way, but a long and often useless way... See comments above.
note there is a slight difference between this variable 'var Number' and the variable 'var numTen' from above. 'var Number' is declared as an number _object_, while var numTen is a variable containing a number.
The Number object is rarely necessary and the only usefull purpose is to collect its properties into one object (eg: so you can make your own properties for a number object using protoype) and to allow numbers to be converted into strings via the toString method.
[/OFF TOPIC]