JavaScript

What is the difference between == and === ?
The 3 equal signs mean "equality without type coercion". Using the triple equals, the values must be equal in type as well. 

== is equal to 
=== is exactly equal to (value and type)
0==false // true 
0===false // false, because they are of a different type
1=="1" // true, auto type coercion
1==="1" // false, because they are of a different type
How do I format a number to a dollar amount in JavaScript?
function formatDollar(num) {
    var p = num.toFixed(2).split(".");
    return "$" + p[0].split("").reverse().reduce(function(acc, num, i, orig) {
        return  num + (i && !(i % 3) ? "," : "") + acc;
    }, "") + "." + p[1];
}

formatDollar(45664544.23423) // "$45,664,544.23"
formatDollar(45) // "$45.00"
formatDollar(123) // "$123.00"
formatDollar(7824) // "$7,824.00"
formatDollar(1) // "$1.00"

No comments:

Post a Comment