var num = "10 popular JavaScript methods";
alert( parseInt(num) ); // 10
Tip: check returned values with isNaN() to avoid parsing errors.
var num = "I learn 10 popular JavaScript methods"; // would return NaN
num = num.replace( /^\D+/, "" ); // Delete non-numbers from start
alert( parseInt(num) ); // 10!
var color = "FF"; // Found in #FF0000 (red)
alert( parseInt( color , 16 ) ); // Returns 255
Caveat: without radix as second argument, "0x" is considered base 16, and leading zeros as potential octal numbers (base 8). Explicitly use "parseInt(someNumber,10)" to be safe.
$num = '3.14'; // String containing a floating point
echo intval( $num ); // 3
Sister function: parseFloat() extracts floating point values from strings.