This JavaScript article tutorial is not written by a JavaScript expert, but it still contains many helpful JavaScript tips and tricks to improve your JavaScript coding skills.
- Demo
- Enlarge
- Reload
- New window
Free iPage Web Hosting for First Year NOW
If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?
Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
I am not an expert in Javascript. Though, I can share some points which are more significant when you use javascript in your applications.
- Cache element property when access multiple times. In DOM, it's an extensive search of the element to find the same property over and over again. Perfect example is document object
var divelt = document.getElementById("div1″);
var img = document.getElementByTagName("img");
instead use
var doc = document
var divelt = doc.getElementById("div1″);
- Use Local variables rather than Global variables, because local variables are fast, global variables are little performance penalty.
for(i=0; i < array.count; i++){
alert("array data : " +array[i]);
}
store array.count into local variable like count = array.count and use it.
for(i=0; i < count; i++){
alert("array data : " +array[i]);
}
- Don't use eval() when not necessary
- eval statement is expensive in terms of performance
- eval parameters are executed dynamically. So it's hard to understand the program and the program is not more reliable.
- Don't wrap try/catch within loops. - Every catch statement, javascript creates dynamically scope.
- Don't pass function as a string to setTimeout() - setTimeout("myFunction()","") - Internally this will use eval statement instead use function reference like setTimeout(myFunction,....).
- Don't use symbol + for concatenating strings, use String.concat() or Array.join
- Don't use function constructor like new Function() - as equal to eval method.
- Don't use "with" statement. - Used to define the new scope of the element. It is more expensive to look up variables in other scope.
with(document.getElementById("divid").style){
color = '#fff';
width = '150px';
backgroundcolor ='#000′;
}
Javascript has better alternatives for this.
var divobj = document.getElementById("divid");
divobj.style.color = '#fff';
divobj.style.width = '150px';
divobj.style.backgroundcolor = '#000′;
- Cache offsetHeight/offsetWidth before using computation - Every time there is an internal re-flow happening
Re-flow happens at Initial page load, Browser window resize, Layout style changes, Add/Remove DOM nodes.
- Use innerHTML to insert the element into the node
- Sent (0)
- New
Open tool hub for free to use by any one for every one with hundreds of tools
chatGPTaz
Voice chat comning soon. You could talk to ChatGPT and hear the most intelligent AI speaks very soon