Getting Good with JavaScript - Andrew Burgess [16]
toFixed
This method will round your numbers to the number of decimal points you define in the parameter:
Example 3.18
alert( 6..toFixed(4) ); // "6.0000"
alert( 3.14159.toFixed(2) ); // "3.14"
alert( 10.46.toFixed(1) ); // "10.5"
You'll find this useful if you're working with money, and need to round to the nearest cent.
toPrecision
This method is for choosing how many digits should represent your number. This includes numbers of both sides of the decimal point:
Example 3.19
alert( 15.7896.toPrecision(4) ); // "15.79"
alert( 1940..toPrecision(2) ); // "1.9e+3"
Wait, what? How does that last example work? Well, it round the number a precision of 2, which in this case is to the hundreds digit. Then, it converts it to exponential notation.
I'll add here that all three of these number methods don't changed the variable with the number; they return a new value that you can assign to another (or the same) variable:
Example 3.20
var a = 300,
b = a.toFixed(2);
console.log(a, typeof a); // 300
console.log(b, typeof b); // "300.00"
You may also have noticed that all three of these methods return strings, not numbers. What are you to do if you want to convert them back to numbers? You can use the global function parseInt and parseFloat. You use parseInt when you want to get a whole number (parse integer). You can hand it a string, and if it can make sense of a number within it, it will do so. For example:
Example 3.21
alert( parseInt("123", 10) ); // 123
alert( parseInt("45.67", 10) ); // 45
alert( parseInt("$12.00", 10) ); // NaN
parseInt will start at the beginning of the string and stop when the character isn't a number. That's why you get "Not a Number" from a string starting with "$." The second parameter is the radix: the base of the number system we want the string to be parsed to. So 10 is decimal, 2 is binary, 8 is octal, and so on.
parseFloat can take decimals and exponential forms:
Example 3.22
alert( parseFloat("45.67", 10) ); // 45.67
alert( parseFloat(1940..toPrecision(2), 10) ); // 1900
Just like parseInt, parseFloat stops when the character is not a number, decimal point, or exponential notation.
Date Methods
There are a ton of methods for Date objects, but some are used way more often than others.
get____
You'll probably use the family of get_____ methods most often, for getting different pieces of the date. Here are the specifics; you'll notice that most of the values are zero-based:
Method Name Return Value
getDate day of month, 1 - 31
getDay day of week, 0 - 6
getFullYear year
getHours hour, 0 - 23
getMilliseconds milliseconds, 0 - 999
getMinutes minutes, 0 - 59
getMonth month, 0 - 11
getSeconds seconds, 0 - 59
getTime milliseconds since January 1, 1970
getTimezoneOffset difference between GMT and local time, in minutes
set____
Most of the get methods have a corresponding set method. They each take a single parameter, identical to the return value of their get counterpart. Here are your options:
setDate
setFullYear
setHours
setMilliseconds
setMinute
setMonth
setSeconds
setTime
parse
Sometimes you'll have a string date that you want to convert to a Date object. Date.parse will do that for you. It actually converts it to a number, the number of milliseconds since midnight on January 1, 1970 (known as the Unix epoch). Then, you can plug that number into new Date() to get a Date object.
Example 3.23
alert( Date.parse("June 18, 1970") ); // 14529600000
alert( Date.parse("2010/11/11") ); // 1289451600000
var d = new Date(Date.parse("1995/04/25")); // Tue Apr 25 1995 00:00:00 GMT-0400 (EST)
alert(d);
Array Methods
Arrays have useful methods as well. Let's check out some of the common ones!
join
This is the reverse of the string's split method. It will join all the elements in the array into a string. You can pass a single parameter to join that will be put between each element in the array.
Example 3.24
alert( ["cats", "dogs", "hamsters", "fish"].join(' ') ); // "cats dogs hamsters fish"
alert( "this should