Skip to content

Instantly share code, notes, and snippets.

@formatkaka
Last active August 17, 2017 19:10
Show Gist options
  • Save formatkaka/d81775cf474ac0a12b32cb7e8cc7f4d5 to your computer and use it in GitHub Desktop.
Save formatkaka/d81775cf474ac0a12b32cb7e8cc7f4d5 to your computer and use it in GitHub Desktop.
Javascript best practices
////////////// 1 ////////////
//If using `parseInt` always specify the radix.
//Example - If we want to convert some string to number (assuming we aren't sure about what the string formaat can be.)
//If we want to convert it to decimal (radix - 10), we generally use
var mystring = '10';
var myinvalidstring = '0xff';
parseInt(mystring) // --> 10
parseInt(myinvalidstring) // --> 255 assumed radix to be hex because string starts with 0x
//Therefore, use it like:
parseInt(myinvalidstring) // --> 0`
////////// 2 ///////////
// If either value (aka side) in a comparison could be the true or
// false value, avoid == and use ===.
// Examples : to be added
// • If either value in a comparison could be of these specific values
// (0, "", or []—empty array), avoid == and use ===
// Examples : to be added
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment