SlideShare a Scribd company logo
Style guide
javascript and best
practices Part 1
How i am ?
Belgacem Ben Ltaif
Full stack developer & Front End Technical Lead
Founder & Cto Powerful Consulting
twitter @gasston_egass
github @gasston
Email : belgacem.benltaif@gmail.com
Objects
1.
“ Use the literal
syntax for object
creation.
// bad
const item = new
Object();
// good
const item = {};
“ Use computed
property names
when creating
objects with
dynamic property
names.
// good
const obj = {
id: 5,
name: 'San Francisco',
[getKey('enabled')]: true,
};
“ Use property value
shorthand.
const foo = 'Luke';
// bad
const obj = {foo: foo,};
// good
const obj = {foo};
“ Only quote
properties that are
invalid identifiers
// bad
const bad = {'foo': 3, 'bar':
4,'data-blah': 5,};
// good
const good = {
foo: 3,
bar: 4,
'data-blah': 5,
};
“Do not call Object.prototype methods directly,
such as hasOwnProperty, proumerable, and
isPrototyppertyIsEneOf.
“// bad
console.log(object.hasOwnProperty(key));
// good
console.log(Object.prototype.hasOwnProperty.call(object, key));
// best
const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope.
/* or */
import has from 'has';
// ...
console.log(has.call(object, key));
Functions
2.
“Use named function
expressions instead
of function
declarations.
// bad
function foo() {
}
const foo = function () {
// ...
};
// good
const foo = function bar() {
// ...
};
“ Wrap immediately
invoked function
expressions in
parentheses
// immediately-invoked
function expression (IIFE)
(function () {
console.log('Welcome to
the Internet. Please follow
me.');
}());
“ Never name a
parameter arguments.
This will take
precedence over the
arguments object that
is given to every
function scope.
// bad
function foo(name, options,
arguments) {
// ...
}
// good
function foo(name, options,
args) {
// ...
}
“Use default parameter syntax rather than
mutating function arguments.
// really bad
function handleThings(opts) { opts = opts || {};
// ...
}
function handleThings(opts) {
if (opts === void 0) {
opts = {};
}
// good
function handleThings(opts = {}) {
// ...
}
Worning !!
Never declare a function in a non-function block (if, while, etc). Assign
the function to a variable instead. Browsers will allow you to do it, but
they all interpret it differently, which is bad news bears.
thanks!
Any questions?
twitter @gasston_egass
github @gasston
Email : belgacem.benltaif@gmail.com

More Related Content

PPTX
Node.js Lightning Talk
PDF
Automate your docs, automate yourself
PDF
Swift Meetup HH 2016/09
PDF
Amur river
PPTX
Que agua estas tomando?
ODP
The Essential Perl Hacker's Toolkit
PPTX
Relacion de la economia con otras ciencias
PPT
Indonesia After 2014 by Greg Barton
Node.js Lightning Talk
Automate your docs, automate yourself
Swift Meetup HH 2016/09
Amur river
Que agua estas tomando?
The Essential Perl Hacker's Toolkit
Relacion de la economia con otras ciencias
Indonesia After 2014 by Greg Barton

Viewers also liked (13)

PDF
Ponencia Seguridad de Datos
PPTX
World Trade Center Quad
PDF
Portfolio pvc
PDF
College to Confidence
PPTX
Austin Journal of Clinical Immunology
PPTX
SharePoint meetup Speaking Deck - Knowing the formula
PPTX
Blogging, tweeting, sharing your work to reach policy makers
PPTX
تمضي قوافل الشهداء و يبقى الخائن يعيش في
PDF
Befargo ru
PDF
Cardinal Édifice remporte le chantier de l’Institut Mines-Télécom du Campus P...
PDF
Fpl gtr-117 secagem
PDF
مجلة إشراقات، العدد 57
PPTX
Project Irrigation
Ponencia Seguridad de Datos
World Trade Center Quad
Portfolio pvc
College to Confidence
Austin Journal of Clinical Immunology
SharePoint meetup Speaking Deck - Knowing the formula
Blogging, tweeting, sharing your work to reach policy makers
تمضي قوافل الشهداء و يبقى الخائن يعيش في
Befargo ru
Cardinal Édifice remporte le chantier de l’Institut Mines-Télécom du Campus P...
Fpl gtr-117 secagem
مجلة إشراقات، العدد 57
Project Irrigation
Ad

Similar to Javascript part1 (20)

PDF
Javascript the New Parts v2
PPT
Advanced JavaScript
PPT
Beginning Object-Oriented JavaScript
PDF
JavaScript Inheritance
PPT
JavaScript In Object Oriented Way
PDF
Metaprogramming in JavaScript
PPTX
Javascript Objects Deep Dive
PDF
New c sharp3_features_(linq)_part_i
PPT
Presentation
PPT
JavaScript Workshop
PPTX
Cucumber From the Ground Up - Joseph Beale
PDF
properties-how-do-i - Transcript.pdf
PDF
Week 9 IUB c#
PDF
Building .NET-based Applications with C#
PDF
Javascript closures
 
PPTX
Web technology javascript
PPT
Object
PDF
Video WebChat Conference Tool
PDF
C++ interview questions
Javascript the New Parts v2
Advanced JavaScript
Beginning Object-Oriented JavaScript
JavaScript Inheritance
JavaScript In Object Oriented Way
Metaprogramming in JavaScript
Javascript Objects Deep Dive
New c sharp3_features_(linq)_part_i
Presentation
JavaScript Workshop
Cucumber From the Ground Up - Joseph Beale
properties-how-do-i - Transcript.pdf
Week 9 IUB c#
Building .NET-based Applications with C#
Javascript closures
 
Web technology javascript
Object
Video WebChat Conference Tool
C++ interview questions
Ad

Recently uploaded (20)

PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
cuic standard and advanced reporting.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Spectroscopy.pptx food analysis technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Big Data Technologies - Introduction.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation_ Review paper, used for researhc scholars
Network Security Unit 5.pdf for BCA BBA.
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Dropbox Q2 2025 Financial Results & Investor Presentation
Diabetes mellitus diagnosis method based random forest with bat algorithm
Per capita expenditure prediction using model stacking based on satellite ima...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Machine learning based COVID-19 study performance prediction
Spectral efficient network and resource selection model in 5G networks
cuic standard and advanced reporting.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Spectroscopy.pptx food analysis technology
Advanced methodologies resolving dimensionality complications for autism neur...
Chapter 3 Spatial Domain Image Processing.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Big Data Technologies - Introduction.pptx

Javascript part1

  • 1. Style guide javascript and best practices Part 1
  • 2. How i am ? Belgacem Ben Ltaif Full stack developer & Front End Technical Lead Founder & Cto Powerful Consulting twitter @gasston_egass github @gasston Email : belgacem.benltaif@gmail.com
  • 4. “ Use the literal syntax for object creation. // bad const item = new Object(); // good const item = {};
  • 5. “ Use computed property names when creating objects with dynamic property names. // good const obj = { id: 5, name: 'San Francisco', [getKey('enabled')]: true, };
  • 6. “ Use property value shorthand. const foo = 'Luke'; // bad const obj = {foo: foo,}; // good const obj = {foo};
  • 7. “ Only quote properties that are invalid identifiers // bad const bad = {'foo': 3, 'bar': 4,'data-blah': 5,}; // good const good = { foo: 3, bar: 4, 'data-blah': 5, };
  • 8. “Do not call Object.prototype methods directly, such as hasOwnProperty, proumerable, and isPrototyppertyIsEneOf.
  • 9. “// bad console.log(object.hasOwnProperty(key)); // good console.log(Object.prototype.hasOwnProperty.call(object, key)); // best const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope. /* or */ import has from 'has'; // ... console.log(has.call(object, key));
  • 11. “Use named function expressions instead of function declarations. // bad function foo() { } const foo = function () { // ... }; // good const foo = function bar() { // ... };
  • 12. “ Wrap immediately invoked function expressions in parentheses // immediately-invoked function expression (IIFE) (function () { console.log('Welcome to the Internet. Please follow me.'); }());
  • 13. “ Never name a parameter arguments. This will take precedence over the arguments object that is given to every function scope. // bad function foo(name, options, arguments) { // ... } // good function foo(name, options, args) { // ... }
  • 14. “Use default parameter syntax rather than mutating function arguments. // really bad function handleThings(opts) { opts = opts || {}; // ... } function handleThings(opts) { if (opts === void 0) { opts = {}; } // good function handleThings(opts = {}) { // ... }
  • 15. Worning !! Never declare a function in a non-function block (if, while, etc). Assign the function to a variable instead. Browsers will allow you to do it, but they all interpret it differently, which is bad news bears.
  • 16. thanks! Any questions? twitter @gasston_egass github @gasston Email : belgacem.benltaif@gmail.com