Skip to content

Instantly share code, notes, and snippets.

@ishenli
Last active August 29, 2015 14:17
Show Gist options
  • Save ishenli/15d3b238aada4c9e070a to your computer and use it in GitHub Desktop.
Save ishenli/15d3b238aada4c9e070a to your computer and use it in GitHub Desktop.
JavaScript Tips
// Verify that a given argument is a number
function isNumber(n){
return !isNaN(parseFloat(n)) && isFinite(n);
}
// Get the max or the min in an array of numbers
var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
var maxInNumbers = Math.max.apply(Math, numbers);
var minInNumbers = Math.min.apply(Math, numbers);
// Truncate an array using length
var myArray = [12 , 222 , 1000 , 124 , 98 , 10 ];
myArray.length = 4; // myArray will be equal to [12 , 222 , 1000 , 124]
// Rounding number to N decimal place
var num =2.443242342;
num = num.toFixed(4); // num will be equal to 2.4432
// Floating point problems
0.1 + 0.2 === 0.3 // is false , is 0.30000000000000004
9007199254740992 + 1 // is equal to 9007199254740992
9007199254740992 + 2 // is equal to 9007199254740994
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment