Here I collected some JavaScript Tips & Tricks and JavaScript Best Practices which can be used in our daily projects or bookmark it for reference purpose.
This includes some useful JavaScript snippets too.
These JavaScript Tips And Tricks are I collected from some books and Google.
[] is better than “new Array();”:
This is from the book “JavaScript: Good Parts”.
“A common error in JavaScript programs is to use an object when an array is required or an array when an object is required. The rule is simple: when the property names are small sequential integers, you should use an array. Otherwise, use an object.”
Instead of using
Use the below code snippet
Append an array to existing Array in JavaScript:
The following JavaScript code snippet will append array ‘a’ to ‘b’. We can use concat() method for the same but concat() method creates new array but Array Prototype push append to existing array itself
Check If An Object Is Array in JavaScript:
In JavaScript we can use type of to check the type of a variable but we cannot use type of for array.The below JavaScript code snippet can be used for array detection.In ECMAScript 5 we can use Array.isArray(obj) but this ECMAScript not widely adopted.
I wrote some other code but that is not efficient as above code snippet.The concept here is the array length cannot be negative but when you are forced to do it it throws an exception.
Truncate array in JavaScript:
Most of them use splice or slice method to truncate but we can use JavaScript length property to do the same. And it is more efficient than slice or splice method Here is the performance result . JavaScript slice or splice methods will return new array this might be the reason for the performance degradation
JavaScript array to CSV:
To convert Java Script array to CSV format comma separated values you can use below code snippet.UseJavaScript valueOf() method for comma separated values
And if you want to use pipe instead as deliminator you can use JavaScript join() method as shown below
If you want to convert CSV to Array use following code
Remove array element by Index or value in JavaScript:
If you want to remove array element by Index use the below JavaScript code snippet. We can use slice() method.
To remove element by value use the below JavaScript code snippet
Capture browser close Event:
To invalidate the session of an user when the user close the browser, we can use the below code snippet. In JavaScript we have events such as onbeforeunload and onunload. These events triggered whenever the page gets unload i.e., if user close the browser or move to different page.
Redirect webpage in JavaScript:
The below JavaScript code snippet used to perform http redirect on a given URL.
Declare Variables Outside of the For Statement:
This is the most common mistake done by most of the developers observer the below code snippet
In each iteration we must determine array length and also we should traverse through the DOM to find container instead of this assign array length and DOM elements to variables before for loop.So use the below JavaScript code snippet.
Shorten the JavaScript Code snippets:
Apart from the above code snippets and JavaScript Tips And Tricks here are the some tips to improve the code quality.
These are all “false” in boolean expressions:
null
undefined
''
or empty string- 0 the number
and below are all true:
"0"
the string[]
the empty array{}
the empty object
i.e., instead of using below code
we can use the following JavaScript code(as long as you don’t expect x to be 0, or the empty string, or false)
And to check the empty string or null we might be using this below code snippet
Instead of this we can simply use the below JavaScript code snippet
Conditional Operator in JavaScript:
Use conditional operator instead of if else loop in JavaScript
We can replace the above code with conditional operator
Use OR (“||”) operator and AND(“&&”) operators in JavaScript:
OR(“||”) Operator can be called as ‘default’ operator because instead of using below JavaScript code
Use OR “||” operator to assign default value
Use the ‘&&’ operator to shorten the code
replace the above code with ‘&&’ operator
And see the below example for LogIn and Registration we might be using below JavaScript snippet
You can replace entire code in one single line using || and && operators
I hope you have enjoyed this article.If so share this post with your friends and also join our mailing list.
And if you have any other JavaScript Tips & Tricks or JavaScript Best Practices share with us.
No comments:
Post a Comment