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
Generate your business videos by AI with voice or just text
Your first FREE AI Video App! Automate Your First AI Video. Create Your Professional Video In 5 Minutes By AI No Equipment Or Video Editing Skill Requred. Effortless Video Production For Content Marketers.
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
Generate your business videos by AI with voice or just text
chatGPTaz.com
Talk to ChatGPT by your mother language
AppAIVideo
Your first FREE AI Video App
Deepfake Video
Deepfake AI Video Maker
Deepfake
Deepfake AI Video Maker
AI Deep Fake
Deepfake AI Video Maker
AIvidio
AI Video Mobile Solutions
AIvideos
AI Video Platform & Solutions
AIvedio
AI Video App Maker
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Powerful AI Presentation PPT Maker for FREE
Build an impressive presentation with our free online AI presentation app
Your next top AI Assistant
Claude AI, developed by Anthropic
Your next top AI Assistant
Claude AI, developed by Anthropic
Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing
Dall-E-OpenAI.com
Generate creative images automatically with AI
chatGPT4.win
Talk to ChatGPT by your mother language
First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language
Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools
GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project
iPhoneKer.com
Save up to 630$ when buy new iPhone 16
Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k