Useful JavaScript Tips, Tricks and Best Practices
1 – Don’t forget var keyword when assigning a variable’s value for the first time.
var keyword when assigning a variable’s value for the first time.2 – use === instead of ==
=== instead of ==[10] === 10 // is false
[10] == 10 // is true
'10' == 10 // is true
'10' === 10 // is false
[] == 0 // is true
[] === 0 // is false
'' == false // is true but true == "a" is false
'' === false // is false 3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy.
undefined, null, 0, false, NaN, '' (empty string) are all falsy.4 – Use Semicolons for line termination
5 – Create an object constructor
6 – Be careful when using typeof, instanceof and constructor.
typeof, instanceof and constructor.7 – Create a Self-calling Function
8 – Get a random item from an array
9 – Get a random number in a specific range
10 – Generate an array of numbers with numbers from 0 to max
11 – Generate a random set of alphanumeric characters
12 – Shuffle an array of numbers
13 – A string trim function
14 – Append an array to another array
15 – Transform the arguments object into an array
arguments object into an array16 – Verify that a given argument is a number
17 – Verify that a given argument is an array
18 – Get the max or the min in an array of numbers
19 – Empty an array
20 – Don’t use delete to remove an item from array
21 – Truncate an array using length
22 – Use logical AND/ OR for conditions
23 – Use the map() function method to loop through an array’s items
24 – Rounding number to N decimal place
25 – Floating point problems
26 – Check the properties of an object when using a for-in loop
27 – Comma operator
28 – Cache variables that need calculation or querying
29 – Verify the argument before passing it to isFinite()
isFinite()30 – Avoid negative indexes in arrays
31 – Serialization and deserialization (working with JSON)
32 – Avoid the use of eval() or the Function constructor
eval() or the Function constructor33 – Avoid using with() (The good part)
with() (The good part)34 – Avoid using for-in loop for arrays
35 – Pass functions, not strings, to setTimeout() and setInterval()
setTimeout() and setInterval()36 – Use a switch/case statement instead of a series of if/else
37 – Use switch/case statement with numeric ranges
38 – Create an object whose prototype is a given object
39 – An HTML escaper function
40 – Avoid using try-catch-finally inside a loop
41 – Set timeouts to XMLHttpRequests
XMLHttpRequests42 – Deal with WebSocket timeout
43 – Keep in mind that primitive operations can be faster than function calls. Use VanillaJS.
44 – Don’t forget to use a code beautifier when coding. Use JSLint and minification (JSMin, for example) before going live.
45. Replace All
46. Extract Unique Values
47. Convert number to string
48. Convert string to number
49. Shuffle elements from array
50. Flatten multidimensional array
51. Short Circuit Conditionals
52. Dynamic Property Names
53. Use length to resize/empty an array
Last updated