SlideShare a Scribd company logo
JS Best practices
@EEsparaquia
Use “;” & Curly Braces
if(someVariable)
x = false
----------------------------------------------------------------------------------------
if(someVariable) {
x = false;
}
anotherFunction();
Always Declare Local Variables
¤  All variables used in a function should be declared as
local variables.
¤  Local variables must be declared with the var keyword,
otherwise they will become global variables.
Declarations on Top
Avoid Global Variables
¤  Global variables and functions can be overwritten by
other scripts.
¤  Use local variables instead, and learn how to use
closures.
Declarations on Top
Initialize & meaningful variables
// Declare and initiate at the beginning
var firstName = "",
lastName = "",
price = 0,
discount = 0,
fullPrice = 0,
myArray = [],
myObject = {};
Beware of Automatic Type
Conversions
var x = "Hello"; // typeof x is a string
x = 5; // changes typeof x to a number
----------------------------------------------------------------------------------------
var x = 5 + 7; // x.valueOf() is 12, typeof x is a number
var x = 5 + "7"; // x.valueOf() is 57, typeof x is a string
Beware of Automatic Type
Conversions
var x = "5" + 7; // x.valueOf() is 57, typeof x is a string
var x = 5 - 7; // x.valueOf() is -2, typeof x is a number
var x = 5 - "7"; // x.valueOf() is -2, typeof x is a number
var x = "5" - 7; // x.valueOf() is -2, typeof x is a number
var x = 5 - "x"; // x.valueOf() is NaN, typeof x is a number
Comparisons “==” vs “===”
¤  The == operator will compare for equality after doing any
necessary type conversions (to matching types).
¤  The === operator forces comparison of values and type
----------------------------------------------------------------------------------------
0 === ""; // false
1 === "1"; // false
1 === true; // false
0 == ""; // true
1 == "1"; // true
1 == true; // true
Place Scripts at the Bottom Page
<p>And you don’t know JS yet. </p>
<script type="text/javascript" src="path/to/file.js"></script>
<script type="text/javascript" src="path/to/
anotherFile.js"></script>
</body>
</html>
End Switches with Defaults
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
default:
day = "Unknown";
}
Advantages
Advantages
¤  There is no need to install the application on each machine.
¤  New versions of software can be released on a regular basis without needing users
to follow an upgrade process. In fact some web applications have daily releases
of software.
¤  Web applications can be hosted on cloud based infrastructure, leading to higher
availability, and lower costs.
¤  The same web application can be used on multiple devices, including handheld
devices”
¤  Fragmento de: Dane Cameron. “A Software Engineer Learns HTML5, JavaScript
and jQuery: A guide to standards-based web applications”. iBooks.
Dont’s
Accidentally Using the Assignment
Operator
var x = 0;
1.  if (x == 10)
2.  if (x = 10)
3.  If (x = 0)
Confusing Addition & Concatenation
var x = 10 + 5; // the result in x is 15
var x = 10 + "5"; // the result in x is "105"
Breaking a JavaScript String
var x =
"Hello World!";
_________________________________________________________
var x = "Hello
World!";
Ending an Array & Object Definition
with a Comma
points = [40, 100, 1, 5, 25, 10,];
person = {firstName:"John", lastName:"Doe", age:46,}
Undefined is Not Null
if (myObj !== null && typeof myObj !== "undefined")
¤  Correct
if (typeof myObj !== "undefined" && myObj !== null)

More Related Content

PDF
Broadleaf Presents Thymeleaf
PPTX
AngulrJS Overview
PPTX
10 useful WordPress functions (and maybe more)
PPTX
Ch2(working with forms)
PDF
Introduction to thymeleaf
PDF
Thymeleaf Introduction
PDF
Human Talks - StencilJS
PPTX
Let's write secure drupal code!
Broadleaf Presents Thymeleaf
AngulrJS Overview
10 useful WordPress functions (and maybe more)
Ch2(working with forms)
Introduction to thymeleaf
Thymeleaf Introduction
Human Talks - StencilJS
Let's write secure drupal code!

What's hot (18)

PPTX
AngularJS Compile Process
PPTX
5. hello popescu
PPTX
AngularJS Architecture
PDF
drupal_static presentation 20160510
PPTX
AngularJS Directives
PDF
In-depth changes to Drupal 8 javascript
PDF
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
PPTX
Java script functions
PPTX
Javascript void
PDF
AngularJS Basic Training
PDF
AngularJS Basics with Example
PPT
Practical Ext JS Debugging
PPTX
AngularJS Services
PDF
CGI::Prototype (NPW 2006)
PPT
symfony & jQuery (phpDay)
PPT
PDF
hachioji.pm #40 : asynchronous in JS
PPTX
Building an End-to-End AngularJS Application
AngularJS Compile Process
5. hello popescu
AngularJS Architecture
drupal_static presentation 20160510
AngularJS Directives
In-depth changes to Drupal 8 javascript
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Java script functions
Javascript void
AngularJS Basic Training
AngularJS Basics with Example
Practical Ext JS Debugging
AngularJS Services
CGI::Prototype (NPW 2006)
symfony & jQuery (phpDay)
hachioji.pm #40 : asynchronous in JS
Building an End-to-End AngularJS Application
Ad

Viewers also liked (17)

PDF
Crrn am nov2011
PDF
Förslag till Nordisk standard för revision i mindre företag (SASE) - Mina kri...
PDF
Toiminimet KPL työryhmän esityksessä. Kommenttini 23.3.2015
PDF
The Digital Self: Sport fan identity formation in the digital
PDF
Comparing SASE (Nordic Audit Standard for Small Entities) and ISA 230 documen...
PDF
Rinkside Tweeting: A Foucauldian Analysis of Changing Power Relations in the ...
PPTX
Avery-one Has An Opinion: Twitter, Same-Sex Marriage, and the NHL
PDF
Social Media and Non-Profits
PDF
Crrn presentation pg
PDF
Tilintarkastuskertomusvertailu Suomi ja UK - Pieni yritys
PDF
Rinkside tweeting
PDF
2011 08-30 rural policy events calendar - future events
PDF
Upcoming events 2011-01-25
PDF
Crrn rdn august12_2010
PPTX
Tweeting the Game: Is live-tweeting reshaping the NHL fandom experience
PDF
Crrn rdn meeting2011_final
PDF
How to post_01
Crrn am nov2011
Förslag till Nordisk standard för revision i mindre företag (SASE) - Mina kri...
Toiminimet KPL työryhmän esityksessä. Kommenttini 23.3.2015
The Digital Self: Sport fan identity formation in the digital
Comparing SASE (Nordic Audit Standard for Small Entities) and ISA 230 documen...
Rinkside Tweeting: A Foucauldian Analysis of Changing Power Relations in the ...
Avery-one Has An Opinion: Twitter, Same-Sex Marriage, and the NHL
Social Media and Non-Profits
Crrn presentation pg
Tilintarkastuskertomusvertailu Suomi ja UK - Pieni yritys
Rinkside tweeting
2011 08-30 rural policy events calendar - future events
Upcoming events 2011-01-25
Crrn rdn august12_2010
Tweeting the Game: Is live-tweeting reshaping the NHL fandom experience
Crrn rdn meeting2011_final
How to post_01
Ad

Similar to Js c1 best practices (20)

RTF
Java scripts
PPT
PHP Unit Testing
PDF
Seam Glassfish Slidecast
PPT
CSIS 138 JavaScript Class2
KEY
Plone Interactivity
TXT
R57.Php
KEY
Building and Distributing PostgreSQL Extensions Without Learning C
PDF
Java Web Programming [5/9] : EL, JSTL and Custom Tags
PDF
Javascript basics
PPTX
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
PDF
PHP and Rich Internet Applications
PPTX
Workshop 1: Good practices in JavaScript
PPT
JavaScript
PPT
Jstl Guide
PDF
ETL Patterns with Postgres
PDF
Jscript Fundamentals
KEY
Bestpractices nl
PPTX
Java script for web developer
PDF
JavaScript Beginner Tutorial | WeiYuan
PDF
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
Java scripts
PHP Unit Testing
Seam Glassfish Slidecast
CSIS 138 JavaScript Class2
Plone Interactivity
R57.Php
Building and Distributing PostgreSQL Extensions Without Learning C
Java Web Programming [5/9] : EL, JSTL and Custom Tags
Javascript basics
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
PHP and Rich Internet Applications
Workshop 1: Good practices in JavaScript
JavaScript
Jstl Guide
ETL Patterns with Postgres
Jscript Fundamentals
Bestpractices nl
Java script for web developer
JavaScript Beginner Tutorial | WeiYuan
javascript-variablesanddatatypes-130218094831-phpapp01.pdf

Recently uploaded (20)

PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
PDF
CapCut Video Editor 6.8.1 Crack for PC Latest Download (Fully Activated) 2025
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
AutoCAD Professional Crack 2025 With License Key
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Download FL Studio Crack Latest version 2025 ?
PPTX
assetexplorer- product-overview - presentation
Oracle Fusion HCM Cloud Demo for Beginners
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
CapCut Video Editor 6.8.1 Crack for PC Latest Download (Fully Activated) 2025
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
Weekly report ppt - harsh dattuprasad patel.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
wealthsignaloriginal-com-DS-text-... (1).pdf
AutoCAD Professional Crack 2025 With License Key
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Computer Software and OS of computer science of grade 11.pptx
Advanced SystemCare Ultimate Crack + Portable (2025)
Digital Systems & Binary Numbers (comprehensive )
Odoo Companies in India – Driving Business Transformation.pdf
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
Download FL Studio Crack Latest version 2025 ?
assetexplorer- product-overview - presentation

Js c1 best practices

  • 2. Use “;” & Curly Braces if(someVariable) x = false ---------------------------------------------------------------------------------------- if(someVariable) { x = false; } anotherFunction();
  • 3. Always Declare Local Variables ¤  All variables used in a function should be declared as local variables. ¤  Local variables must be declared with the var keyword, otherwise they will become global variables. Declarations on Top
  • 4. Avoid Global Variables ¤  Global variables and functions can be overwritten by other scripts. ¤  Use local variables instead, and learn how to use closures. Declarations on Top
  • 5. Initialize & meaningful variables // Declare and initiate at the beginning var firstName = "", lastName = "", price = 0, discount = 0, fullPrice = 0, myArray = [], myObject = {};
  • 6. Beware of Automatic Type Conversions var x = "Hello"; // typeof x is a string x = 5; // changes typeof x to a number ---------------------------------------------------------------------------------------- var x = 5 + 7; // x.valueOf() is 12, typeof x is a number var x = 5 + "7"; // x.valueOf() is 57, typeof x is a string
  • 7. Beware of Automatic Type Conversions var x = "5" + 7; // x.valueOf() is 57, typeof x is a string var x = 5 - 7; // x.valueOf() is -2, typeof x is a number var x = 5 - "7"; // x.valueOf() is -2, typeof x is a number var x = "5" - 7; // x.valueOf() is -2, typeof x is a number var x = 5 - "x"; // x.valueOf() is NaN, typeof x is a number
  • 8. Comparisons “==” vs “===” ¤  The == operator will compare for equality after doing any necessary type conversions (to matching types). ¤  The === operator forces comparison of values and type ---------------------------------------------------------------------------------------- 0 === ""; // false 1 === "1"; // false 1 === true; // false 0 == ""; // true 1 == "1"; // true 1 == true; // true
  • 9. Place Scripts at the Bottom Page <p>And you don’t know JS yet. </p> <script type="text/javascript" src="path/to/file.js"></script> <script type="text/javascript" src="path/to/ anotherFile.js"></script> </body> </html>
  • 10. End Switches with Defaults switch (new Date().getDay()) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; default: day = "Unknown"; }
  • 12. Advantages ¤  There is no need to install the application on each machine. ¤  New versions of software can be released on a regular basis without needing users to follow an upgrade process. In fact some web applications have daily releases of software. ¤  Web applications can be hosted on cloud based infrastructure, leading to higher availability, and lower costs. ¤  The same web application can be used on multiple devices, including handheld devices” ¤  Fragmento de: Dane Cameron. “A Software Engineer Learns HTML5, JavaScript and jQuery: A guide to standards-based web applications”. iBooks.
  • 14. Accidentally Using the Assignment Operator var x = 0; 1.  if (x == 10) 2.  if (x = 10) 3.  If (x = 0)
  • 15. Confusing Addition & Concatenation var x = 10 + 5; // the result in x is 15 var x = 10 + "5"; // the result in x is "105"
  • 16. Breaking a JavaScript String var x = "Hello World!"; _________________________________________________________ var x = "Hello World!";
  • 17. Ending an Array & Object Definition with a Comma points = [40, 100, 1, 5, 25, 10,]; person = {firstName:"John", lastName:"Doe", age:46,}
  • 18. Undefined is Not Null if (myObj !== null && typeof myObj !== "undefined") ¤  Correct if (typeof myObj !== "undefined" && myObj !== null)