Number

From PlainOldWebserver

Jump to: navigation, search

This converts a non-number object to a number. It is interesting that Javascript is both dynamically typed and uses '+' for addition and string concatenation. Difficulties arise that Number() fixes.

 var this_date = "2006-12-19";
 var m = this_date.match(/^(\d{4})-(\d{2})/);
 var not_next_year = m[1] + 1;
 document.writeln("Next year is not "+not_next_year);
 var next_year = Number(m[1])+1;
 document.writeln("Next year is " + next_year);


Result:


 Next year is not 20061
 Next year is 2007
Personal tools
Navigation