SlideShare a Scribd company logo
1 of 19
Download to read offline
Javascript Best
Practices
Code safe, Code fast
if (type == ‘student’) {
access = true;
} else {
access = false;
}
Ordinary Javascript Code
if (isTrue == true) {
doRightThing();
} else {
cleanYourMess();
}
Function call Using Ternary Operator
(access == ‘student’) ? alert(“he’s student”) : alert(“outsider”);
(access == ‘student’) ? ( count++, alert(‘hi’)) : ( noAccess(), alert(‘no’));
Multiple Expression
Using Ternary Operator
access = (type == ‘student’) ? true : false;
Using Logical Operator in assignment
function(num) {
num = num || “default”;
}
Using OR
function(num) {
if(num == undefined) {
num = “default”;
}
}
default argument
function(status) {
if (status ==true) {
status =false;
} else {
status = true;
}
}
Switch on / Switch off
Using NOT
function(status) {
status = !status;
}
var user = [
name : "Arkar",
children: [ "Hla Hla","Mya Mya","Aung Aung","Tun Tun”],
wife : "Phyo Phyo",
parents: ["U U ", "Daw Daw"]
getParent:function(){
return this.parent;
},
getwife:function(){
return this.wife;
}
];
Sample Object
for (var i=0; i<user.children.length; i++) {

console.log(user.children[i]);
}
loop for children
for (var i=0; i<user.children.length; i++) {

console.log(user.children[i]);
}
inside Loop
- access user object
- access children property
- access length property
- access user object
- access children property
- access children of i
- execute console.log
Total result is 1 + 7 x 4 = 29
var children = user.children;
for (var i=0; i<children.length; i++) {

console.log(children[i]);
}
- access user object
- access children property
- assign children
Outside Loop inside Loop
- access children
- access length;
- access children;
- access i of children
- console.log
Total result is 1 + 3 + 5 * 4 = 24
Before Optimisation After Optimisation
4
Count
29 24
10
(7x+1) (5x+4)
71 54
100 701 504
1000 7001 5004
var children = user.children;
var length = children.length;
for (var i=0; i<length; i++) {

console.log(children[i]);
}
- access user object
- access children property
- assign children
- access children
- access length
- assign length
Outside Loop inside Loop
- access length
- access children;
- access i of children
- console.log
Total result is 1 + 6 + 4* 4 = 23
Case A Case B
4
Count
29 24
10
(7x+1) (5x+4)
71 54
100 701 504
1000 7001 5004
23
(4x+7)
47
407
4007
Case C
Case A
Case B
Case C
Comparison Operator
‘4’ == 4 //true
true == 1 //true
== ===
‘4’ === 4 //false
true === 1 //false
false == 0 //true false === 0 //false
“/n /t /n” == 0 //true “/n /t /n” === 0 //false
Avoid with
with(user){
var wife = getwife(); // Phyo Phyo
console.log(children); //"Hla Hla","Mya Mya","Aung Aung","Tun Tun”
console.log(parent); // “U U”, “Daw Daw”
}
with(user){
var getNewWife = function() {
this.wife = “Zune Thinzar”;
};
};
If He want new wife
// Global Scope
var getNewWife = function() {
this.wife = “Zune Thinzar”;
};
But end up as Global Scope
Unlucky Arkar, Sorry to hear that
So, don’t try to cheat on “with”
Javascript Number
Use toFixed()
Script.js
var list = user.getwife();
function getChildren(user) {
var list = user.children;
list.forEach(function(child){
console.log(child);
});
}
Script2.js
<html>
<head>
<script src=“script.js”>
<script src=“script2.js”>
HTML
Namespace
var script1 = {
list: user.getwife(),
doChore : function() {
for (var i of this.list) {
console.log(this.list[i] + “cleaning”);
}
}.
deleteWife: function () {
delete this.list;
}
var script2 = {
list: user.children;
getChildren: function(user) {
this.list.forEach(function(child){
console.log(child);
});
},
cleanChildren:function(){
///
}
Script.js Script2.js
The End
- slideshare.net/nainglinaung91
- linkedin.com/in/nainglinaung
- https://twitter.com/kelvinm0rRis
- https://github.com/nainglinaung
Naing Lin Aung is currently work as Programmer in Aceplus Solution. If you want to
contact, you can check with the following links

More Related Content

What's hot

Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ ConstructorSomenath Mukhopadhyay
 
[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3Seok-joon Yun
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling Intro C# Book
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
Dti2143 lab sheet 8
Dti2143 lab sheet 8Dti2143 lab sheet 8
Dti2143 lab sheet 8alish sha
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScriptJohannes Hoppe
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScriptJohannes Hoppe
 
Python Conditionals and Functions
Python Conditionals and FunctionsPython Conditionals and Functions
Python Conditionals and FunctionsPooja B S
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingNeeru Mittal
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitmfrost503
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
rtrtrNew text document
rtrtrNew text documentrtrtrNew text document
rtrtrNew text documentHarish Khodke
 

What's hot (15)

Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ Constructor
 
[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
C programming
C programmingC programming
C programming
 
Dti2143 lab sheet 8
Dti2143 lab sheet 8Dti2143 lab sheet 8
Dti2143 lab sheet 8
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript
 
Python Conditionals and Functions
Python Conditionals and FunctionsPython Conditionals and Functions
Python Conditionals and Functions
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
rtrtrNew text document
rtrtrNew text documentrtrtrNew text document
rtrtrNew text document
 

Similar to Javascript Best Practice

JSConf: All You Can Leet
JSConf: All You Can LeetJSConf: All You Can Leet
JSConf: All You Can Leetjohndaviddalton
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptAbimbola Idowu
 
Stuff you didn't know about action script
Stuff you didn't know about action scriptStuff you didn't know about action script
Stuff you didn't know about action scriptChristophe Herreman
 
04 Advanced Javascript
04 Advanced Javascript04 Advanced Javascript
04 Advanced Javascriptcrgwbr
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxsrcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxwhitneyleman54422
 
JavaScript for Web Analysts
JavaScript for Web AnalystsJavaScript for Web Analysts
JavaScript for Web AnalystsLukáš Čech
 
jQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyjQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyHuiyi Yan
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesAnkit Rastogi
 
OO JS for AS3 Devs
OO JS for AS3 DevsOO JS for AS3 Devs
OO JS for AS3 DevsJason Hanson
 
Javascript best practices
Javascript best practicesJavascript best practices
Javascript best practicesManav Gupta
 
Javascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introductionJavascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introductionIban Martinez
 
Introduction to ECMAScript 2015
Introduction to ECMAScript 2015Introduction to ECMAScript 2015
Introduction to ECMAScript 2015Tomasz Dziuda
 
YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
 YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docxgertrudebellgrove
 
C++ Programming - 5th Study
C++ Programming - 5th StudyC++ Programming - 5th Study
C++ Programming - 5th StudyChris Ohk
 
About java
About javaAbout java
About javaJay Xu
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 

Similar to Javascript Best Practice (20)

JSConf: All You Can Leet
JSConf: All You Can LeetJSConf: All You Can Leet
JSConf: All You Can Leet
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Stuff you didn't know about action script
Stuff you didn't know about action scriptStuff you didn't know about action script
Stuff you didn't know about action script
 
Say It With Javascript
Say It With JavascriptSay It With Javascript
Say It With Javascript
 
04 Advanced Javascript
04 Advanced Javascript04 Advanced Javascript
04 Advanced Javascript
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxsrcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
 
JavaScript for Web Analysts
JavaScript for Web AnalystsJavaScript for Web Analysts
JavaScript for Web Analysts
 
jQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyjQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journey
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
 
OO JS for AS3 Devs
OO JS for AS3 DevsOO JS for AS3 Devs
OO JS for AS3 Devs
 
Javascript best practices
Javascript best practicesJavascript best practices
Javascript best practices
 
Javascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introductionJavascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introduction
 
Introduction to ECMAScript 2015
Introduction to ECMAScript 2015Introduction to ECMAScript 2015
Introduction to ECMAScript 2015
 
Functional JS+ ES6.pptx
Functional JS+ ES6.pptxFunctional JS+ ES6.pptx
Functional JS+ ES6.pptx
 
JavaScript Gotchas
JavaScript GotchasJavaScript Gotchas
JavaScript Gotchas
 
YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
 YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
 
C++ Programming - 5th Study
C++ Programming - 5th StudyC++ Programming - 5th Study
C++ Programming - 5th Study
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
 
About java
About javaAbout java
About java
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 

Recently uploaded

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 

Recently uploaded (20)

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 

Javascript Best Practice

  • 2. if (type == ‘student’) { access = true; } else { access = false; } Ordinary Javascript Code if (isTrue == true) { doRightThing(); } else { cleanYourMess(); }
  • 3. Function call Using Ternary Operator (access == ‘student’) ? alert(“he’s student”) : alert(“outsider”); (access == ‘student’) ? ( count++, alert(‘hi’)) : ( noAccess(), alert(‘no’)); Multiple Expression Using Ternary Operator access = (type == ‘student’) ? true : false;
  • 4. Using Logical Operator in assignment function(num) { num = num || “default”; } Using OR function(num) { if(num == undefined) { num = “default”; } } default argument
  • 5. function(status) { if (status ==true) { status =false; } else { status = true; } } Switch on / Switch off Using NOT function(status) { status = !status; }
  • 6. var user = [ name : "Arkar", children: [ "Hla Hla","Mya Mya","Aung Aung","Tun Tun”], wife : "Phyo Phyo", parents: ["U U ", "Daw Daw"] getParent:function(){ return this.parent; }, getwife:function(){ return this.wife; } ]; Sample Object for (var i=0; i<user.children.length; i++) {
 console.log(user.children[i]); } loop for children
  • 7. for (var i=0; i<user.children.length; i++) {
 console.log(user.children[i]); } inside Loop - access user object - access children property - access length property - access user object - access children property - access children of i - execute console.log Total result is 1 + 7 x 4 = 29
  • 8. var children = user.children; for (var i=0; i<children.length; i++) {
 console.log(children[i]); } - access user object - access children property - assign children Outside Loop inside Loop - access children - access length; - access children; - access i of children - console.log Total result is 1 + 3 + 5 * 4 = 24
  • 9. Before Optimisation After Optimisation 4 Count 29 24 10 (7x+1) (5x+4) 71 54 100 701 504 1000 7001 5004
  • 10. var children = user.children; var length = children.length; for (var i=0; i<length; i++) {
 console.log(children[i]); } - access user object - access children property - assign children - access children - access length - assign length Outside Loop inside Loop - access length - access children; - access i of children - console.log Total result is 1 + 6 + 4* 4 = 23
  • 11. Case A Case B 4 Count 29 24 10 (7x+1) (5x+4) 71 54 100 701 504 1000 7001 5004 23 (4x+7) 47 407 4007 Case C
  • 13. Comparison Operator ‘4’ == 4 //true true == 1 //true == === ‘4’ === 4 //false true === 1 //false false == 0 //true false === 0 //false “/n /t /n” == 0 //true “/n /t /n” === 0 //false
  • 14. Avoid with with(user){ var wife = getwife(); // Phyo Phyo console.log(children); //"Hla Hla","Mya Mya","Aung Aung","Tun Tun” console.log(parent); // “U U”, “Daw Daw” } with(user){ var getNewWife = function() { this.wife = “Zune Thinzar”; }; }; If He want new wife
  • 15. // Global Scope var getNewWife = function() { this.wife = “Zune Thinzar”; }; But end up as Global Scope Unlucky Arkar, Sorry to hear that So, don’t try to cheat on “with”
  • 17. Script.js var list = user.getwife(); function getChildren(user) { var list = user.children; list.forEach(function(child){ console.log(child); }); } Script2.js <html> <head> <script src=“script.js”> <script src=“script2.js”> HTML
  • 18. Namespace var script1 = { list: user.getwife(), doChore : function() { for (var i of this.list) { console.log(this.list[i] + “cleaning”); } }. deleteWife: function () { delete this.list; } var script2 = { list: user.children; getChildren: function(user) { this.list.forEach(function(child){ console.log(child); }); }, cleanChildren:function(){ /// } Script.js Script2.js
  • 19. The End - slideshare.net/nainglinaung91 - linkedin.com/in/nainglinaung - https://twitter.com/kelvinm0rRis - https://github.com/nainglinaung Naing Lin Aung is currently work as Programmer in Aceplus Solution. If you want to contact, you can check with the following links