Page 110 - DCAP202_Fundamentals of Web Programming
P. 110
Unit 8: Programming Constructs in JavaScript
An optional radix parameter can also be used to specify the number system to be used to parse Notes
the string argument. For example,
document.write(parseInt(“10”,16));
Result:
16
16 is the radix value passed, which means the string should be converted from hexadecimal to
decimal value. If the radix is 8, then the string should be converted from octal to decimal value.
If the radix parameter is not specified, then Javascript
Assumes radix to be 16(hexadecimal), if the string starts with “0x”
Assumes radix to be 8(octal), if the string starts with 0
Assumes radix to be 10(decimal), if the string starts with any other number.
parseFloat()
parseFloat() function takes a string as parameter and parses it to a floating point number.
document.write(parseFloat(“10.33”));
document.write(parseFloat(“15 66 75”));
document.write(parseFloat(“this is 77”));
document.write(pareFloat(“ 77 “));
Result:
10.33
15
NaN
77
This function allows leading and trailing spaces. If the first character in the string is not a
number, then it returns NaN. If the string has more than one set of number separated by
delimiters such as spaces, semicolons,commas then it returns only the first set of number before
the first delimiter.
escape()
escape() function encodes the string passed to it so that it can be used across any network, say for
example in query strings.
document.write(escape(“testing escape function!!”));
Result:
testing%20escape%20function%21%21
escape() function leaves digits, latin letters and the characters + - * / . _ @ unchanged and replaces
all other characters with ASCII code of the original character preceded by % symbol.
!
Caution The radix can be any number ranging from 2 to 36 that represents a numeral
system.
LOVELY PROFESSIONAL UNIVERSITY 103