SlideShare a Scribd company logo
Powerful JavaScript TipsPowerful JavaScript Tips
Techniques that all JavaScript programmers can use now
you needn’t be an advanced JavaScript developer to benefit from these tips
Powerful JavaScript TipsPowerful JavaScript Tips
1. “short circuting” method
To set default values, instead of this:
function fileName(name)
​ if (!name) {
name = "unknown_name";
}
return name;
}
Use this:
function fileName(name)
name = name || "unknown_name";
return name;
}
Powerful JavaScript TipsPowerful JavaScript Tips
2. “short circuting” with && (AND)
Instead of this:
function isAdult(age) {
if (age && age > 17) {
return true;
}
​else {
return false;
}
}
Use this:
function isAdult(age) {
return age && age > 17 ;
}
Powerful JavaScript TipsPowerful JavaScript Tips
3. It gets more exciting!
Instead of this:
if (userName) {
logIn(userName);
}
else {
signUp();
}
Use this:
userName && logIn (userName) || signUp ();
Powerful JavaScript TipsPowerful JavaScript Tips
4. powerful uses of immediately invoked function expressions
Immediately and after invoke
(showName = function (name) {
console.log(name || "No Name")
}) (); // No Name​
​
showName ("Mike"); // Mike
Powerful JavaScript TipsPowerful JavaScript Tips
5. when to use immediately invoked function expressions?
All the code is wrapped in the IIFE​
(function () {
​var firstName = “Dragos”, concatenated = undefined;
function init () {
doStuff (firstName);
// code to start the application​
}
​
​function doStuff (firstName) {
concatenated = firstName + 'Developer';
doMoreStuff();
}
​
​function doMoreStuff () {
console.log('Concatenated = ', concatenated;
}
​
​// Start the application
init ();
}) ();
BONUSJavaScript Best Practices
JavaScript Best Practices
1 – Don’t forget var keyword when assigning a variable’s value for the first time.
JavaScript Best Practices
1 – Don’t forget var keyword when assigning a variable’s value for the first time.
2 – use === instead of ==
JavaScript Best Practices
1 – Don’t forget var keyword when assigning a variable’s value for the first time.
2 – use === instead of ==
3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy.
JavaScript Best Practices
1 – Don’t forget var keyword when assigning a variable’s value for the first time.
2 – use === instead of ==
3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy.
4 – Be careful when using typeof, instanceof and constructor.
typeof : a JavaScript unary operator used to return a string that represents the primitive
type of a variable, don’t forget that typeof null will return “object”,
and for the majority of object types (Array, Date, and others) will return also “object”.
constructor : is a property of the internal prototype property, which
could be overridden by code.
instanceof : is another JavaScript operator that check in all the prototypes chain the
constructor it returns true if it’s found and false if not.
JavaScript Best Practices
1 – Don’t forget var keyword when assigning a variable’s value for the first time.
2 – use === instead of ==
3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy.
4 – Be careful when using typeof, instanceof and constructor.
typeof : a JavaScript unary operator used to return a string that represents the primitive
type of a variable, don’t forget that typeof null will return “object”,
and for the majority of object types (Array, Date, and others) will return also “object”.
constructor : is a property of the internal prototype property, which
could be overridden by code.
instanceof : is another JavaScript operator that check in all the prototypes chain the
constructor it returns true if it’s found and false if not.
5 – Get a random item from an array
3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy.
var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119];
var randomItem = items[Math.floor(Math.random() * items.length)];
JavaScript Best Practices
1 – Don’t forget var keyword when assigning a variable’s value for the first time.
2 – use === instead of ==
3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy.
4 – Be careful when using typeof, instanceof and constructor.
typeof : a JavaScript unary operator used to return a string that represents the primitive
type of a variable, don’t forget that typeof null will return “object”,
and for the majority of object types (Array, Date, and others) will return also “object”.
constructor : is a property of the internal prototype property, which
could be overridden by code.
instanceof : is another JavaScript operator that check in all the prototypes chain the
constructor it returns true if it’s found and false if not.
5 – Get a random item from an array
3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy.
var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119];
var randomItem = items[Math.floor(Math.random() * items.length)];
6 – Generate an array of numbers with numbers from 0 to max
var numbersArray = [] , max = 100;
for( var i=1; numbersArray.push(i++) < max;); // numbers = [1,2,3 ... 100]
JavaScript Best Practices
7 – Append an array to another array
var array1 = [12 , "foo" , {name "Joe"} , -2458];
var array2 = ["Doe" , 555 , 100];
Array.prototype.push.apply(array1, array2);
/* array1 will be equal to [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */
JavaScript Best Practices
7 – Append an array to another array`
var array1 = [12 , "foo" , {name "Joe"} , -2458];
var array2 = ["Doe" , 555 , 100];
Array.prototype.push.apply(array1, array2);
/* array1 will be equal to [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */
8 – Transform the arguments object into an array
var argArray = Array.prototype.slice.call(arguments);
JavaScript Best Practices
7 – Append an array to another array`
var array1 = [12 , "foo" , {name "Joe"} , -2458];
var array2 = ["Doe" , 555 , 100];
Array.prototype.push.apply(array1, array2);
/* array1 will be equal to [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */
8 – Transform the arguments object into an array
var argArray = Array.prototype.slice.call(arguments);
9 – Verify that a given argument is a number
function isNumber(n){
return !isNaN(parseFloat(n)) && isFinite(n);
}
JavaScript Best Practices
7 – Append an array to another array`
var array1 = [12 , "foo" , {name "Joe"} , -2458];
var array2 = ["Doe" , 555 , 100];
Array.prototype.push.apply(array1, array2);
/* array1 will be equal to [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */
8 – Transform the arguments object into an array
var argArray = Array.prototype.slice.call(arguments);
9 – Verify that a given argument is a number
function isNumber(n){
return !isNaN(parseFloat(n)) && isFinite(n);
}
10 – Verify that a given argument is an array
function isArray(obj){
return Object.prototype.toString.call(obj) === '[object Array]' ;
}
JavaScript Best Practices
7 – Append an array to another array
var array1 = [12 , "foo" , {name "Joe"} , -2458];
var array2 = ["Doe" , 555 , 100];
Array.prototype.push.apply(array1, array2);
/* array1 will be equal to [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */
8 – Transform the arguments object into an array
var argArray = Array.prototype.slice.call(arguments);
9 – Verify that a given argument is a number
function isNumber(n){
return !isNaN(parseFloat(n)) && isFinite(n);
}
10 – Verify that a given argument is an array
function isArray(obj){
return Object.prototype.toString.call(obj) === '[object Array]' ;
}
11 – Don’t use delete to remove an item from array
Use splice instead of using delete to delete an item from an array. Using delete replaces
the item with undefined instead of the removing it from the array.
Dragos Ionita
Software Engineer
https://ro.linkedin.com/in/dragos-ionita-8ab20756
Dragos Ionita
Software Engineer
https://ro.linkedin.com/in/dragos-ionita-8ab20756
Thanks for watching!Thanks for watching!

More Related Content

What's hot (20)

PDF
Bottom Up
Brian Moschel
 
PDF
Headless Js Testing
Brian Moschel
 
PDF
Performance Optimization and JavaScript Best Practices
Doris Chen
 
PPT
Beginning Object-Oriented JavaScript
Stoyan Stefanov
 
PPTX
Intro to Javascript
Anjan Banda
 
PDF
JavaScript Functions
Colin DeCarlo
 
PPTX
Advanced JavaScript
Nascenia IT
 
PPTX
Javascript basics for automation testing
Vikas Thange
 
PPTX
Oojs 1.1
Rodica Dada
 
PPT
JavaScript Basics
Mats Bryntse
 
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 
PDF
Intro to JavaScript
Jussi Pohjolainen
 
PPTX
Javascript Common Design Patterns
Pham Huy Tung
 
PPT
Object Oriented JavaScript
Donald Sipe
 
PDF
Anonymous functions in JavaScript
Mohammed Sazid Al Rashid
 
PDF
JavaScript Design Patterns
Derek Brown
 
PPT
JavaScript Tutorial
Bui Kiet
 
PPTX
Workshop 1: Good practices in JavaScript
Visual Engineering
 
PPTX
Art of Javascript
Tarek Yehia
 
PDF
JavaScript - From Birth To Closure
Robert Nyman
 
Bottom Up
Brian Moschel
 
Headless Js Testing
Brian Moschel
 
Performance Optimization and JavaScript Best Practices
Doris Chen
 
Beginning Object-Oriented JavaScript
Stoyan Stefanov
 
Intro to Javascript
Anjan Banda
 
JavaScript Functions
Colin DeCarlo
 
Advanced JavaScript
Nascenia IT
 
Javascript basics for automation testing
Vikas Thange
 
Oojs 1.1
Rodica Dada
 
JavaScript Basics
Mats Bryntse
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 
Intro to JavaScript
Jussi Pohjolainen
 
Javascript Common Design Patterns
Pham Huy Tung
 
Object Oriented JavaScript
Donald Sipe
 
Anonymous functions in JavaScript
Mohammed Sazid Al Rashid
 
JavaScript Design Patterns
Derek Brown
 
JavaScript Tutorial
Bui Kiet
 
Workshop 1: Good practices in JavaScript
Visual Engineering
 
Art of Javascript
Tarek Yehia
 
JavaScript - From Birth To Closure
Robert Nyman
 

Viewers also liked (17)

PDF
Web Application Development Tools for Creating Perfect User Experience
ChromeInfo Technologies
 
PPTX
vExpert 2014 Certificate by Nathan Gusti Ryan
Nathan Gusti Ryan
 
PDF
Cross Functional Alignment Around the Customer Processes to Drive Success
Gainsight
 
PPTX
vExpert 2015 Certificate by Nathan Gusti Ryan
Nathan Gusti Ryan
 
PPTX
Jose celi
joseceli1999
 
PDF
Grow Smart Program Outline
Ervin Gruia
 
PPTX
Thames valley prevention powerpoint n.1 v2
James Carter
 
PPTX
Creative designer and front end developer - Two faces of the same coin
Web Designers Nepal
 
PDF
Ben Brauneck
Wettbewerb
 
PPT
El carnestoltes
gvendre3
 
PDF
Del dicho al hecho: analizando proyectos
Disonancias
 
PPTX
Socrative - Gamification in education - Manu Melwin Joy
manumelwin
 
PPTX
Tooling for the productive front-end developer
Maurice De Beijer [MVP]
 
PPTX
Globalizacion en Ecuador by Diana
diana lozada gonzalez
 
PDF
Google Chrome developer tools
Daniel Siepmann
 
PDF
Smart Sales: entendiendo el proceso de venta
wpargentina
 
PDF
Por qué unirse a la comunidad global de WordPress y cómo ser parte del movimi...
wpargentina
 
Web Application Development Tools for Creating Perfect User Experience
ChromeInfo Technologies
 
vExpert 2014 Certificate by Nathan Gusti Ryan
Nathan Gusti Ryan
 
Cross Functional Alignment Around the Customer Processes to Drive Success
Gainsight
 
vExpert 2015 Certificate by Nathan Gusti Ryan
Nathan Gusti Ryan
 
Jose celi
joseceli1999
 
Grow Smart Program Outline
Ervin Gruia
 
Thames valley prevention powerpoint n.1 v2
James Carter
 
Creative designer and front end developer - Two faces of the same coin
Web Designers Nepal
 
Ben Brauneck
Wettbewerb
 
El carnestoltes
gvendre3
 
Del dicho al hecho: analizando proyectos
Disonancias
 
Socrative - Gamification in education - Manu Melwin Joy
manumelwin
 
Tooling for the productive front-end developer
Maurice De Beijer [MVP]
 
Globalizacion en Ecuador by Diana
diana lozada gonzalez
 
Google Chrome developer tools
Daniel Siepmann
 
Smart Sales: entendiendo el proceso de venta
wpargentina
 
Por qué unirse a la comunidad global de WordPress y cómo ser parte del movimi...
wpargentina
 
Ad

Similar to Powerful JavaScript Tips and Best Practices (20)

PPTX
JavaScript Proven Practises
Robert MacLean
 
PPTX
The JavaScript Programming Language
Mohammed Irfan Shaikh
 
PPTX
Learn java script
Mahmoud Asadi
 
PPTX
Front end fundamentals session 1: javascript core
Web Zhao
 
PPTX
JavaScript gotchas
Pierre Nallet
 
PPTX
Javascript best practices
Manav Gupta
 
PPTX
JavaScript / Web Engineering / Web Development / html + css + js/presentation
M Sajid R
 
PPTX
Java script
Adrian Caetano
 
PDF
Js hacks
Nishchit Dhanani
 
PPTX
Java script advance-auroskills (2)
BoneyGawande
 
KEY
JavaScript Neednt Hurt - JavaBin talk
Thomas Kjeldahl Nilsson
 
PPSX
Javascript variables and datatypes
Varun C M
 
PDF
Fewd week5 slides
William Myers
 
PPTX
Module 2 Javascript. Advanced concepts of javascript
BKReddy3
 
PDF
GDI Seattle - Intro to JavaScript Class 2
Heather Rock
 
PPTX
Functional Programming in Javascript - IL Tech Talks week
yoavrubin
 
PPTX
Wintellect - Devscovery - Enterprise JavaScript Development 1 of 2
Jeremy Likness
 
PDF
JavaScript Array Interview Questions PDF By ScholarHat
Scholarhat
 
PPTX
Javascript 101
Shlomi Komemi
 
PDF
JavaScript and jQuery - Web Technologies (1019888BNR)
Beat Signer
 
JavaScript Proven Practises
Robert MacLean
 
The JavaScript Programming Language
Mohammed Irfan Shaikh
 
Learn java script
Mahmoud Asadi
 
Front end fundamentals session 1: javascript core
Web Zhao
 
JavaScript gotchas
Pierre Nallet
 
Javascript best practices
Manav Gupta
 
JavaScript / Web Engineering / Web Development / html + css + js/presentation
M Sajid R
 
Java script
Adrian Caetano
 
Java script advance-auroskills (2)
BoneyGawande
 
JavaScript Neednt Hurt - JavaBin talk
Thomas Kjeldahl Nilsson
 
Javascript variables and datatypes
Varun C M
 
Fewd week5 slides
William Myers
 
Module 2 Javascript. Advanced concepts of javascript
BKReddy3
 
GDI Seattle - Intro to JavaScript Class 2
Heather Rock
 
Functional Programming in Javascript - IL Tech Talks week
yoavrubin
 
Wintellect - Devscovery - Enterprise JavaScript Development 1 of 2
Jeremy Likness
 
JavaScript Array Interview Questions PDF By ScholarHat
Scholarhat
 
Javascript 101
Shlomi Komemi
 
JavaScript and jQuery - Web Technologies (1019888BNR)
Beat Signer
 
Ad

More from Dragos Ionita (7)

PDF
Reactive programming - Observable
Dragos Ionita
 
PDF
Adventures with Angular 2
Dragos Ionita
 
PDF
The new way to write a frontend software
Dragos Ionita
 
ODP
Robotics and Arduino (Arduino UNO)
Dragos Ionita
 
PDF
Html5 - Awesome APIs
Dragos Ionita
 
PDF
Hybrid Mobile Application with Ionic Framework
Dragos Ionita
 
PDF
Google Tag Manager (GTM)
Dragos Ionita
 
Reactive programming - Observable
Dragos Ionita
 
Adventures with Angular 2
Dragos Ionita
 
The new way to write a frontend software
Dragos Ionita
 
Robotics and Arduino (Arduino UNO)
Dragos Ionita
 
Html5 - Awesome APIs
Dragos Ionita
 
Hybrid Mobile Application with Ionic Framework
Dragos Ionita
 
Google Tag Manager (GTM)
Dragos Ionita
 

Recently uploaded (20)

PDF
Advancing WebDriver BiDi support in WebKit
Igalia
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
Designing Production-Ready AI Agents
Kunal Rai
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Advancing WebDriver BiDi support in WebKit
Igalia
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Designing Production-Ready AI Agents
Kunal Rai
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 

Powerful JavaScript Tips and Best Practices

  • 1. Powerful JavaScript TipsPowerful JavaScript Tips Techniques that all JavaScript programmers can use now you needn’t be an advanced JavaScript developer to benefit from these tips
  • 2. Powerful JavaScript TipsPowerful JavaScript Tips 1. “short circuting” method To set default values, instead of this: function fileName(name) ​ if (!name) { name = "unknown_name"; } return name; } Use this: function fileName(name) name = name || "unknown_name"; return name; }
  • 3. Powerful JavaScript TipsPowerful JavaScript Tips 2. “short circuting” with && (AND) Instead of this: function isAdult(age) { if (age && age > 17) { return true; } ​else { return false; } } Use this: function isAdult(age) { return age && age > 17 ; }
  • 4. Powerful JavaScript TipsPowerful JavaScript Tips 3. It gets more exciting! Instead of this: if (userName) { logIn(userName); } else { signUp(); } Use this: userName && logIn (userName) || signUp ();
  • 5. Powerful JavaScript TipsPowerful JavaScript Tips 4. powerful uses of immediately invoked function expressions Immediately and after invoke (showName = function (name) { console.log(name || "No Name") }) (); // No Name​ ​ showName ("Mike"); // Mike
  • 6. Powerful JavaScript TipsPowerful JavaScript Tips 5. when to use immediately invoked function expressions? All the code is wrapped in the IIFE​ (function () { ​var firstName = “Dragos”, concatenated = undefined; function init () { doStuff (firstName); // code to start the application​ } ​ ​function doStuff (firstName) { concatenated = firstName + 'Developer'; doMoreStuff(); } ​ ​function doMoreStuff () { console.log('Concatenated = ', concatenated; } ​ ​// Start the application init (); }) ();
  • 8. JavaScript Best Practices 1 – Don’t forget var keyword when assigning a variable’s value for the first time.
  • 9. JavaScript Best Practices 1 – Don’t forget var keyword when assigning a variable’s value for the first time. 2 – use === instead of ==
  • 10. JavaScript Best Practices 1 – Don’t forget var keyword when assigning a variable’s value for the first time. 2 – use === instead of == 3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy.
  • 11. JavaScript Best Practices 1 – Don’t forget var keyword when assigning a variable’s value for the first time. 2 – use === instead of == 3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy. 4 – Be careful when using typeof, instanceof and constructor. typeof : a JavaScript unary operator used to return a string that represents the primitive type of a variable, don’t forget that typeof null will return “object”, and for the majority of object types (Array, Date, and others) will return also “object”. constructor : is a property of the internal prototype property, which could be overridden by code. instanceof : is another JavaScript operator that check in all the prototypes chain the constructor it returns true if it’s found and false if not.
  • 12. JavaScript Best Practices 1 – Don’t forget var keyword when assigning a variable’s value for the first time. 2 – use === instead of == 3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy. 4 – Be careful when using typeof, instanceof and constructor. typeof : a JavaScript unary operator used to return a string that represents the primitive type of a variable, don’t forget that typeof null will return “object”, and for the majority of object types (Array, Date, and others) will return also “object”. constructor : is a property of the internal prototype property, which could be overridden by code. instanceof : is another JavaScript operator that check in all the prototypes chain the constructor it returns true if it’s found and false if not. 5 – Get a random item from an array 3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy. var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119]; var randomItem = items[Math.floor(Math.random() * items.length)];
  • 13. JavaScript Best Practices 1 – Don’t forget var keyword when assigning a variable’s value for the first time. 2 – use === instead of == 3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy. 4 – Be careful when using typeof, instanceof and constructor. typeof : a JavaScript unary operator used to return a string that represents the primitive type of a variable, don’t forget that typeof null will return “object”, and for the majority of object types (Array, Date, and others) will return also “object”. constructor : is a property of the internal prototype property, which could be overridden by code. instanceof : is another JavaScript operator that check in all the prototypes chain the constructor it returns true if it’s found and false if not. 5 – Get a random item from an array 3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy. var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119]; var randomItem = items[Math.floor(Math.random() * items.length)]; 6 – Generate an array of numbers with numbers from 0 to max var numbersArray = [] , max = 100; for( var i=1; numbersArray.push(i++) < max;); // numbers = [1,2,3 ... 100]
  • 14. JavaScript Best Practices 7 – Append an array to another array var array1 = [12 , "foo" , {name "Joe"} , -2458]; var array2 = ["Doe" , 555 , 100]; Array.prototype.push.apply(array1, array2); /* array1 will be equal to [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */
  • 15. JavaScript Best Practices 7 – Append an array to another array` var array1 = [12 , "foo" , {name "Joe"} , -2458]; var array2 = ["Doe" , 555 , 100]; Array.prototype.push.apply(array1, array2); /* array1 will be equal to [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */ 8 – Transform the arguments object into an array var argArray = Array.prototype.slice.call(arguments);
  • 16. JavaScript Best Practices 7 – Append an array to another array` var array1 = [12 , "foo" , {name "Joe"} , -2458]; var array2 = ["Doe" , 555 , 100]; Array.prototype.push.apply(array1, array2); /* array1 will be equal to [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */ 8 – Transform the arguments object into an array var argArray = Array.prototype.slice.call(arguments); 9 – Verify that a given argument is a number function isNumber(n){ return !isNaN(parseFloat(n)) && isFinite(n); }
  • 17. JavaScript Best Practices 7 – Append an array to another array` var array1 = [12 , "foo" , {name "Joe"} , -2458]; var array2 = ["Doe" , 555 , 100]; Array.prototype.push.apply(array1, array2); /* array1 will be equal to [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */ 8 – Transform the arguments object into an array var argArray = Array.prototype.slice.call(arguments); 9 – Verify that a given argument is a number function isNumber(n){ return !isNaN(parseFloat(n)) && isFinite(n); } 10 – Verify that a given argument is an array function isArray(obj){ return Object.prototype.toString.call(obj) === '[object Array]' ; }
  • 18. JavaScript Best Practices 7 – Append an array to another array var array1 = [12 , "foo" , {name "Joe"} , -2458]; var array2 = ["Doe" , 555 , 100]; Array.prototype.push.apply(array1, array2); /* array1 will be equal to [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */ 8 – Transform the arguments object into an array var argArray = Array.prototype.slice.call(arguments); 9 – Verify that a given argument is a number function isNumber(n){ return !isNaN(parseFloat(n)) && isFinite(n); } 10 – Verify that a given argument is an array function isArray(obj){ return Object.prototype.toString.call(obj) === '[object Array]' ; } 11 – Don’t use delete to remove an item from array Use splice instead of using delete to delete an item from an array. Using delete replaces the item with undefined instead of the removing it from the array.
  • 19. Dragos Ionita Software Engineer https://ro.linkedin.com/in/dragos-ionita-8ab20756 Dragos Ionita Software Engineer https://ro.linkedin.com/in/dragos-ionita-8ab20756 Thanks for watching!Thanks for watching!