This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var now = new Date(); | |
| now.toLocaleString(); // Tue Jun 14 2011 11:32:39 GMT-0400 (Eastern Daylight Time) | |
| now.toGMTString(); // Tue, 14 Jun 2011 15:32:39 GMT (useful for setting browser cookies) | |
| now.getTime(); // 1308065603585 (Unix epoch time in milliseconds since midnight of January 1, 1970) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| parseInt(x, 10) // 10 = decimal radix | |
| parseFloat(x, 10) | |
| x.toFixed(2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Math.PI // 3.141592653589793 | |
| Math.E // 2.718281828459045 | |
| Math.LN10 // 2.302585092994046 | |
| Math.LN2 // 0.6931471805599453 | |
| Math.LOG10E // 0.4342944819032518 | |
| Math.LOG2E // 1.4426950408889634 | |
| Math.SQRT1_2 // 0.7071067811865476 | |
| Math.SQRT2 // 1.4142135623730951 | |
| Math.random() // returns a number between 0 and 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| try { | |
| } | |
| catch (ex) { | |
| console.error(ex); | |
| } | |
| finally { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| switch (letter) { | |
| case "a": | |
| case "e": | |
| case "i": | |
| case "o": | |
| case "u": | |
| // is a vowel | |
| break; | |
| case "y": | |
| // is sometimes a vowel |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var row = (cnt % 2 === 1) ? "odd" : "even"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if () { | |
| } | |
| else if () { | |
| } | |
| else { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // fast | |
| var i = myArr.length; | |
| while (i--) { | |
| console.log(i + ': ' + myArr[i]); | |
| } | |
| // while loop | |
| var i = 0; | |
| while (i < myArr.length) { | |
| console.log(i + ': ' + myArr[i]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for (var i = 0; i < myArr.length; i++) { | |
| // | |
| } | |
| for (var i = 0, ii = myArr.length; i < ii; i++) { | |
| // 20% faster | |
| } | |
| for (var i = myArr.length; i--;) { | |
| // 50% faster in reverse |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var myInt = 0, | |
| myFloat = 0.99, | |
| myOct = 0123, // 83 | |
| myHex = 0xFFF, // 4095 | |
| myBigExp = 9.8e6, // 9800000 | |
| mySmallExp = 9.8e-6, // 0.0000098 | |
| myMax = Number.MAX_VALUE, // 1.7976931348623157e+308 | |
| myMin = Number.MIN_VALUE, // 5e-324 | |
| myStr = "Hello", | |
| myBool = false, |