SlideShare a Scribd company logo
13 JAVASCRIPT TECHNIQUES TO IMPROVE
YOUR CODE
by Vikash Gupta | Aug 17, 2017 | Mobile development, Web Development | 0 comments
We all write codes in di erent ways with a di erent style and most of the times forgot
about standardization, let’s discuss JavaScript Techniques to Improve Your Code.
This post includes the top tips to write JavaScript code, that would make your code Apple of
one’s eye. This post is referred to both i.e. beginner to javascript as well as experienced
javascript developers.
For newbie javascript developers, Javascript is a client-side scripting language. If you want
to use it on server-side, you have to use some speci c framework like Nodejs. The World of
javascript has been changed since Nodejs framework has come into action.
For an experienced developer, you know that almost all web applications use Javascript
as primary, you should know about these tips. These tips will make your code clean.
Understand that, when I say clean code, that points to the code that is:
1. Easy to understand, no matter that code is given to some other developer.
2. Easy to maintain, means small changes can be made
U a
So, I am going to share these tips, that I have learned during development. These tips will
start with an easy to advanced level, So don’t be bored If you already know the starting
ones.
1. Uses of proper variable name:
This is the rst tip that you have to learn if you want to be the best developer. A variable
name should be relevant to the function. These are some convention I follow during
naming:
Variable and function names are written as camelCase.
Global variables are written in UPPERCASE.
Constants (like PI) written in UPPERCASE.
2. Uses of proper text-indent in code snippets:
Your code might have good logic with proper variable naming but there is a question. Is
the code easy to read? Giving a proper text-indent means providing right space in each
line.
There is some good example for it.
3. The power of comments:
The only thing that can make your code more reusable is the way you use comments for it.
If you don’t know how to write the proper comments, then your life as a programmer will
be in danger J. In above example, you can see. In the following tips, I will elaborate more
about comments.
4. No need to use var for multiple times:
4. No need to use var for multiple times:
Exactly, There is no need to use ‘var’ to initialize multiple variables. Instead of, you can
initialize in following way-
5. The power of Comma Operator:
For making your code small, you can use comma operator in following way-
6. Use of ‘===’ instead of ‘==’
Most of the programmer know about it but somehow they always forget to use it. But it
makes a large di erence in what they code and lls their code like bugs.
For javascript newbies, ‘==’ is used in time of comparison, when you want to compare only
values, and use ‘===’ when you want to compare a variable value as well as its data type.
So use these comparison variables wisely.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Not Recommended
 
Var a=10;
 
Var b=7;
 
Var c=12;
 
// Recommended
 
Var a=10,
 
b=7,
 
c=12;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Standard way
 
var x=1,
 
x++,
 
y=100;
 
//I recommend
 
var x=1,
 
y=( x++,100 );
 
console.log(x); // x will be equal to 2.
 
console.log(y);// y will be equal to 100;
For example- For javascript, if(‘1’==1) statement will return true value and if(‘1’===1) returns
false.
7. Magic of ‘use strict’
This statement gives your code the power that you can’t use undeclared variables. This
makes your code hard to break and easy to maintain. You can use it as following at the
above of each program le-
8. Verify variable Before use:
Before using an argument passed to function, you should check its data type. Otherwise,
your code will break. There is a javascript function to check each and every data type
named ‘type of’. You can use it in following way-
For more information, go to https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference/Operators/typeof.
9. Use for loop in an optimized way:
A normal for loop looks like this, but this doesn’t mean that we should do it in exactly the
same way. We all should avoid this syntax and try as following-
1 “use strict”;
1
2
3
4
5
6
7
8
9
10
11
// for Number check as
 
type of (input)===’number’
 
// for string check as
 
Type of (input)===’string’
 
// for Boolean check as
 
type of (input)===’boolean'
1
2
3
4
5
for(var i=0;i< array.length;i++){
 
//Some Code
 
}
As per this above loop, the instantiation of i and length variable is done exactly one time
that makes your code fast.
10. Use of Comparison operators
This is the normal way, you perform a conditional statement. But with help of conditional
operators like ‘&&’ and ’| |’, we can simplify our code more as following way-
11. Use of switch case
I have seen in programs that in the case of multiple conditional statements, they use a
series of the nested if-else statement. That does execute correctly, but it looks somehow
clumsy or hard to understand. So in that condition, we should use switch case statements.
For more information on switch-case, go to https://www.w3schools.com/js/js_switch.asp.
12. Uses of Immediately Invoke function expression(IIFE)( Self-calling
1
2
3
4
for(var i=0; length=array.length,i<length;i++){
// Some code
 
}
1
2
3
4
5
6
7
var a;
 
If(!a){  // if a do not exists
 
a=’India’;
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// I Recommended as
 
var a;
 
a=””||”India”;
 
You can use ‘&&’ in following way-
 
// A Standard Way
 
If(a){  // if a exists
 
Console.log(‘America will grow’);
 
}
 
// But you can do as
 
a && console.log(‘America will grow’);
12. Uses of Immediately Invoke function expression(IIFE)( Self-calling
function)
An IIFE is a function that executes automatically where you create them in the program.
They don’t have any name.
13. Use Javascript tools
At the end, there are a number of websites that you can to make your code beautiful and
error free. I will point to these three pillars, that helped me a lot.
1. https://codebeautify.org/jsvalidate
This website will help you with javascript validation as well as with proper text-indent.
2. http://lisperator.net/uglifyjs/
This website will help you to provide a mini ed le of your javascript code.
3. http://jshint.com/
This website will provide you a complete analysis of your code with a description,
warnings, and errors.
 
Conclusion:
Now you know how to improve your code with help of Javascript techniques. These
javascript tips not only make your code look clean but also make easy to understand, fast
and optimized.
Habilelabs is a premier software product development and IT-service provider company.
We provide best web based and mobile App solutions.
Hope you found this post pretty useful, So don’t forget to share this post.
1
2
3
4
5
6
7
8
9
var response = (function(){
var x = 0; // in the scope of this function
return function(y){
x += y;
return x;
};
})();
alert(response(10)); //10
alert(response(11)); //21
ContactUs
We are offline
Please add what technique I missed in this post and what is common mistakes you did
most of the times while coding, share with us in the comment.
Share on Facebook Share on Twitter Share on Google+
Share on Linkedin
 Post Views: 63
13 JavaScript Techniques to Improve Your Code
13 Basic Linux Commands for Beginners to Know
4 Bene ts to Use Angular JS With Salesforce
Learning of Less CSS Variables Creation and Uses
What is Meteor.js, a complete tutorial for beginners
3 Simple Steps to Create React JS Components
Recent Posts
Mobile development
Our Partners
Tech stack Migration
Web Development
Categories
Node Js
Angular Js
Android
Technologies
We are offline
Leave us a Message 
Talk to our experts now
Have a project or looking for development team? Contact us.
Ionic
MongoDB
Salesforce
Meteor Js
ios
Windows Phone
Contact Now
Contact Us
Habilelabs © All rights reserved.
Quick Links
 4th Floor, I.G.M. Public School Campus,
Sec-93 Agarwal Farm, Jaipur – 302020
 +91-9828247415
 info@habilelabs.io
About Us
Our Team
Careers
Contact Us
Services
Social Links
Subscribe Newsletter
Email
Subscribe
Web Development
Mobile Development
Tech Stack Migration
Cloud Scaling Production
UI UX Design
Test Driven Development
    
Blog

More Related Content

What's hot

learn mvc project in 7 day
learn mvc project in 7 daylearn mvc project in 7 day
learn mvc project in 7 day
Quach Long
 
Codings Standards
Codings StandardsCodings Standards
Codings Standards
Philip Johnson
 
Commenting Best Practices
Commenting Best PracticesCommenting Best Practices
Commenting Best Practices
mh_azad
 
Knockout js with mvc
Knockout js with mvcKnockout js with mvc
Knockout js with mvc
Joel Cochran
 
Unit 2.4
Unit 2.4Unit 2.4
Switch statement
Switch statementSwitch statement
Switch statement
Patrick John McGee
 
The Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and EverythingThe Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and Everything
PVS-Studio
 
The Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and EverythingThe Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and Everything
Andrey Karpov
 
Mvc Training
Mvc TrainingMvc Training
Vb script
Vb scriptVb script
Vb script
Aamir Sohail
 
Php Condition Flow
Php Condition FlowPhp Condition Flow
Php Condition Flow
lotlot
 
Note - Java Remote Debug
Note - Java Remote DebugNote - Java Remote Debug
Note - Java Remote Debug
boyw165
 
Refactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENGRefactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENG
Luca Minudel
 
클린 테스트
클린 테스트클린 테스트
클린 테스트
Jin Seoung Lee
 
Getting Started with ASP.NET MVC 3 and Razor
Getting Started with ASP.NET MVC 3 and RazorGetting Started with ASP.NET MVC 3 and Razor
Getting Started with ASP.NET MVC 3 and Razor
Dan Wahlin
 
TDD Updated
TDD UpdatedTDD Updated
TDD Updated
Harshit Jain
 
Project AutoMock and Jasmine: Testing Auto-magically!!
Project AutoMock and Jasmine: Testing Auto-magically!!Project AutoMock and Jasmine: Testing Auto-magically!!
Project AutoMock and Jasmine: Testing Auto-magically!!
harshit040591
 
Create first android app with MVVM Architecture
Create first android app with MVVM ArchitectureCreate first android app with MVVM Architecture
Create first android app with MVVM Architecture
khushbu thakker
 
Asp.Net MVC - Razor Syntax
Asp.Net MVC - Razor SyntaxAsp.Net MVC - Razor Syntax
Asp.Net MVC - Razor Syntax
Renier Serven
 
Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010
Christian Heilmann
 

What's hot (20)

learn mvc project in 7 day
learn mvc project in 7 daylearn mvc project in 7 day
learn mvc project in 7 day
 
Codings Standards
Codings StandardsCodings Standards
Codings Standards
 
Commenting Best Practices
Commenting Best PracticesCommenting Best Practices
Commenting Best Practices
 
Knockout js with mvc
Knockout js with mvcKnockout js with mvc
Knockout js with mvc
 
Unit 2.4
Unit 2.4Unit 2.4
Unit 2.4
 
Switch statement
Switch statementSwitch statement
Switch statement
 
The Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and EverythingThe Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and Everything
 
The Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and EverythingThe Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and Everything
 
Mvc Training
Mvc TrainingMvc Training
Mvc Training
 
Vb script
Vb scriptVb script
Vb script
 
Php Condition Flow
Php Condition FlowPhp Condition Flow
Php Condition Flow
 
Note - Java Remote Debug
Note - Java Remote DebugNote - Java Remote Debug
Note - Java Remote Debug
 
Refactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENGRefactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENG
 
클린 테스트
클린 테스트클린 테스트
클린 테스트
 
Getting Started with ASP.NET MVC 3 and Razor
Getting Started with ASP.NET MVC 3 and RazorGetting Started with ASP.NET MVC 3 and Razor
Getting Started with ASP.NET MVC 3 and Razor
 
TDD Updated
TDD UpdatedTDD Updated
TDD Updated
 
Project AutoMock and Jasmine: Testing Auto-magically!!
Project AutoMock and Jasmine: Testing Auto-magically!!Project AutoMock and Jasmine: Testing Auto-magically!!
Project AutoMock and Jasmine: Testing Auto-magically!!
 
Create first android app with MVVM Architecture
Create first android app with MVVM ArchitectureCreate first android app with MVVM Architecture
Create first android app with MVVM Architecture
 
Asp.Net MVC - Razor Syntax
Asp.Net MVC - Razor SyntaxAsp.Net MVC - Razor Syntax
Asp.Net MVC - Razor Syntax
 
Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010
 

Similar to 13 javascript techniques to improve your code

Maintainable Javascript carsonified
Maintainable Javascript carsonifiedMaintainable Javascript carsonified
Maintainable Javascript carsonified
Christian Heilmann
 
You should Know, What are the Common mistakes a node js developer makes?
You should Know, What are the Common mistakes a node js developer makes?You should Know, What are the Common mistakes a node js developer makes?
You should Know, What are the Common mistakes a node js developer makes?
Surendra kumar
 
Ch3- Java Script.pdf
Ch3- Java Script.pdfCh3- Java Script.pdf
Ch3- Java Script.pdf
HASENSEID
 
Unit 4 Java script.pptx
Unit 4 Java script.pptxUnit 4 Java script.pptx
Unit 4 Java script.pptx
Gangesh8
 
Javascript
JavascriptJavascript
Javascript
Nagarajan
 
Top 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppTop 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular App
Katy Slemon
 
Node.JS error handling best practices
Node.JS error handling best practicesNode.JS error handling best practices
Node.JS error handling best practices
Yoni Goldberg
 
Best Practices to Ace ReactJS Web Development!
Best Practices to Ace ReactJS Web Development!Best Practices to Ace ReactJS Web Development!
Best Practices to Ace ReactJS Web Development!
Inexture Solutions
 
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
Cherimay Batallones
 
Javascript Deofuscation A manual Approach
Javascript Deofuscation A manual ApproachJavascript Deofuscation A manual Approach
Javascript Deofuscation A manual Approach
Gregory Hanis
 
JavaScript Core fundamentals - Learn JavaScript Here
JavaScript Core fundamentals - Learn JavaScript HereJavaScript Core fundamentals - Learn JavaScript Here
JavaScript Core fundamentals - Learn JavaScript Here
Laurence Svekis ✔
 
Java script basics
Java script basicsJava script basics
Java script basics
John Smith
 
Introduction to JavaScript Programming
Introduction to JavaScript ProgrammingIntroduction to JavaScript Programming
Introduction to JavaScript Programming
Raveendra R
 
Java 10 - Key Note
Java 10 - Key NoteJava 10 - Key Note
Java 10 - Key Note
Nikhil Hiremath
 
Basic JavaScript Tutorial
Basic JavaScript TutorialBasic JavaScript Tutorial
Basic JavaScript Tutorial
DHTMLExtreme
 
My final requirement
My final requirementMy final requirement
My final requirement
katrinaguevarra29
 
Java script basic
Java script basicJava script basic
Java script basic
Ravi Bhadauria
 
LabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docx
LabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docxLabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docx
LabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docx
DIPESH30
 
JavaScript New Tutorial Class XI and XII.pptx
JavaScript New Tutorial Class XI and XII.pptxJavaScript New Tutorial Class XI and XII.pptx
JavaScript New Tutorial Class XI and XII.pptx
rish15r890
 
How can JAVA Performance tuning speed up applications.pdf
How can JAVA Performance tuning speed up applications.pdfHow can JAVA Performance tuning speed up applications.pdf
How can JAVA Performance tuning speed up applications.pdf
Mindfire LLC
 

Similar to 13 javascript techniques to improve your code (20)

Maintainable Javascript carsonified
Maintainable Javascript carsonifiedMaintainable Javascript carsonified
Maintainable Javascript carsonified
 
You should Know, What are the Common mistakes a node js developer makes?
You should Know, What are the Common mistakes a node js developer makes?You should Know, What are the Common mistakes a node js developer makes?
You should Know, What are the Common mistakes a node js developer makes?
 
Ch3- Java Script.pdf
Ch3- Java Script.pdfCh3- Java Script.pdf
Ch3- Java Script.pdf
 
Unit 4 Java script.pptx
Unit 4 Java script.pptxUnit 4 Java script.pptx
Unit 4 Java script.pptx
 
Javascript
JavascriptJavascript
Javascript
 
Top 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppTop 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular App
 
Node.JS error handling best practices
Node.JS error handling best practicesNode.JS error handling best practices
Node.JS error handling best practices
 
Best Practices to Ace ReactJS Web Development!
Best Practices to Ace ReactJS Web Development!Best Practices to Ace ReactJS Web Development!
Best Practices to Ace ReactJS Web Development!
 
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
 
Javascript Deofuscation A manual Approach
Javascript Deofuscation A manual ApproachJavascript Deofuscation A manual Approach
Javascript Deofuscation A manual Approach
 
JavaScript Core fundamentals - Learn JavaScript Here
JavaScript Core fundamentals - Learn JavaScript HereJavaScript Core fundamentals - Learn JavaScript Here
JavaScript Core fundamentals - Learn JavaScript Here
 
Java script basics
Java script basicsJava script basics
Java script basics
 
Introduction to JavaScript Programming
Introduction to JavaScript ProgrammingIntroduction to JavaScript Programming
Introduction to JavaScript Programming
 
Java 10 - Key Note
Java 10 - Key NoteJava 10 - Key Note
Java 10 - Key Note
 
Basic JavaScript Tutorial
Basic JavaScript TutorialBasic JavaScript Tutorial
Basic JavaScript Tutorial
 
My final requirement
My final requirementMy final requirement
My final requirement
 
Java script basic
Java script basicJava script basic
Java script basic
 
LabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docx
LabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docxLabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docx
LabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docx
 
JavaScript New Tutorial Class XI and XII.pptx
JavaScript New Tutorial Class XI and XII.pptxJavaScript New Tutorial Class XI and XII.pptx
JavaScript New Tutorial Class XI and XII.pptx
 
How can JAVA Performance tuning speed up applications.pdf
How can JAVA Performance tuning speed up applications.pdfHow can JAVA Performance tuning speed up applications.pdf
How can JAVA Performance tuning speed up applications.pdf
 

Recently uploaded

Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Zilliz
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 

Recently uploaded (20)

Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 

13 javascript techniques to improve your code

  • 1. 13 JAVASCRIPT TECHNIQUES TO IMPROVE YOUR CODE by Vikash Gupta | Aug 17, 2017 | Mobile development, Web Development | 0 comments We all write codes in di erent ways with a di erent style and most of the times forgot about standardization, let’s discuss JavaScript Techniques to Improve Your Code. This post includes the top tips to write JavaScript code, that would make your code Apple of one’s eye. This post is referred to both i.e. beginner to javascript as well as experienced javascript developers. For newbie javascript developers, Javascript is a client-side scripting language. If you want to use it on server-side, you have to use some speci c framework like Nodejs. The World of javascript has been changed since Nodejs framework has come into action. For an experienced developer, you know that almost all web applications use Javascript as primary, you should know about these tips. These tips will make your code clean. Understand that, when I say clean code, that points to the code that is: 1. Easy to understand, no matter that code is given to some other developer. 2. Easy to maintain, means small changes can be made U a
  • 2. So, I am going to share these tips, that I have learned during development. These tips will start with an easy to advanced level, So don’t be bored If you already know the starting ones. 1. Uses of proper variable name: This is the rst tip that you have to learn if you want to be the best developer. A variable name should be relevant to the function. These are some convention I follow during naming: Variable and function names are written as camelCase. Global variables are written in UPPERCASE. Constants (like PI) written in UPPERCASE. 2. Uses of proper text-indent in code snippets: Your code might have good logic with proper variable naming but there is a question. Is the code easy to read? Giving a proper text-indent means providing right space in each line. There is some good example for it. 3. The power of comments: The only thing that can make your code more reusable is the way you use comments for it. If you don’t know how to write the proper comments, then your life as a programmer will be in danger J. In above example, you can see. In the following tips, I will elaborate more about comments. 4. No need to use var for multiple times:
  • 3. 4. No need to use var for multiple times: Exactly, There is no need to use ‘var’ to initialize multiple variables. Instead of, you can initialize in following way- 5. The power of Comma Operator: For making your code small, you can use comma operator in following way- 6. Use of ‘===’ instead of ‘==’ Most of the programmer know about it but somehow they always forget to use it. But it makes a large di erence in what they code and lls their code like bugs. For javascript newbies, ‘==’ is used in time of comparison, when you want to compare only values, and use ‘===’ when you want to compare a variable value as well as its data type. So use these comparison variables wisely. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Not Recommended   Var a=10;   Var b=7;   Var c=12;   // Recommended   Var a=10,   b=7,   c=12; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // Standard way   var x=1,   x++,   y=100;   //I recommend   var x=1,   y=( x++,100 );   console.log(x); // x will be equal to 2.   console.log(y);// y will be equal to 100;
  • 4. For example- For javascript, if(‘1’==1) statement will return true value and if(‘1’===1) returns false. 7. Magic of ‘use strict’ This statement gives your code the power that you can’t use undeclared variables. This makes your code hard to break and easy to maintain. You can use it as following at the above of each program le- 8. Verify variable Before use: Before using an argument passed to function, you should check its data type. Otherwise, your code will break. There is a javascript function to check each and every data type named ‘type of’. You can use it in following way- For more information, go to https://developer.mozilla.org/en- US/docs/Web/JavaScript/Reference/Operators/typeof. 9. Use for loop in an optimized way: A normal for loop looks like this, but this doesn’t mean that we should do it in exactly the same way. We all should avoid this syntax and try as following- 1 “use strict”; 1 2 3 4 5 6 7 8 9 10 11 // for Number check as   type of (input)===’number’   // for string check as   Type of (input)===’string’   // for Boolean check as   type of (input)===’boolean' 1 2 3 4 5 for(var i=0;i< array.length;i++){   //Some Code   }
  • 5. As per this above loop, the instantiation of i and length variable is done exactly one time that makes your code fast. 10. Use of Comparison operators This is the normal way, you perform a conditional statement. But with help of conditional operators like ‘&&’ and ’| |’, we can simplify our code more as following way- 11. Use of switch case I have seen in programs that in the case of multiple conditional statements, they use a series of the nested if-else statement. That does execute correctly, but it looks somehow clumsy or hard to understand. So in that condition, we should use switch case statements. For more information on switch-case, go to https://www.w3schools.com/js/js_switch.asp. 12. Uses of Immediately Invoke function expression(IIFE)( Self-calling 1 2 3 4 for(var i=0; length=array.length,i<length;i++){ // Some code   } 1 2 3 4 5 6 7 var a;   If(!a){  // if a do not exists   a=’India’;   } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // I Recommended as   var a;   a=””||”India”;   You can use ‘&&’ in following way-   // A Standard Way   If(a){  // if a exists   Console.log(‘America will grow’);   }   // But you can do as   a && console.log(‘America will grow’);
  • 6. 12. Uses of Immediately Invoke function expression(IIFE)( Self-calling function) An IIFE is a function that executes automatically where you create them in the program. They don’t have any name. 13. Use Javascript tools At the end, there are a number of websites that you can to make your code beautiful and error free. I will point to these three pillars, that helped me a lot. 1. https://codebeautify.org/jsvalidate This website will help you with javascript validation as well as with proper text-indent. 2. http://lisperator.net/uglifyjs/ This website will help you to provide a mini ed le of your javascript code. 3. http://jshint.com/ This website will provide you a complete analysis of your code with a description, warnings, and errors.   Conclusion: Now you know how to improve your code with help of Javascript techniques. These javascript tips not only make your code look clean but also make easy to understand, fast and optimized. Habilelabs is a premier software product development and IT-service provider company. We provide best web based and mobile App solutions. Hope you found this post pretty useful, So don’t forget to share this post. 1 2 3 4 5 6 7 8 9 var response = (function(){ var x = 0; // in the scope of this function return function(y){ x += y; return x; }; })(); alert(response(10)); //10 alert(response(11)); //21 ContactUs We are offline
  • 7. Please add what technique I missed in this post and what is common mistakes you did most of the times while coding, share with us in the comment. Share on Facebook Share on Twitter Share on Google+ Share on Linkedin  Post Views: 63 13 JavaScript Techniques to Improve Your Code 13 Basic Linux Commands for Beginners to Know 4 Bene ts to Use Angular JS With Salesforce Learning of Less CSS Variables Creation and Uses What is Meteor.js, a complete tutorial for beginners 3 Simple Steps to Create React JS Components Recent Posts Mobile development Our Partners Tech stack Migration Web Development Categories Node Js Angular Js Android Technologies We are offline Leave us a Message 
  • 8. Talk to our experts now Have a project or looking for development team? Contact us. Ionic MongoDB Salesforce Meteor Js ios Windows Phone Contact Now Contact Us Habilelabs © All rights reserved. Quick Links  4th Floor, I.G.M. Public School Campus, Sec-93 Agarwal Farm, Jaipur – 302020  +91-9828247415  info@habilelabs.io About Us Our Team Careers Contact Us
  • 9. Services Social Links Subscribe Newsletter Email Subscribe Web Development Mobile Development Tech Stack Migration Cloud Scaling Production UI UX Design Test Driven Development      Blog