SlideShare a Scribd company logo
7
Javascript Hacks for Hipsters
/Tal Bereznitskey @ketacode
Hipster Hack #1 - Method
calling
Trade bloated if/else blocks for a quick function call
//Boring
if(success){
obj.start();
}else{
obj.stop();
}
//Hipster-fun
varmethod=(success?'start':'stop');
obj[method]();
Hipster Hack #2 - String joins
Concatenate two strings or more simpler, easier and faster
['first','name'].join('');//='firstname';
['milk','coffee','sugar'].join(',');//='milk,coffee,sugar'
Hipster Hack #3 - Default
Operator ||
Use the || operator to use the second argument if the first is falsy
//defaultto'Noname'whenmyNameisempty(ornull,orundefined)
varname=myName||'Noname';
//makesurewehaveanoptionsobject
vardoStuff=function(options){
options=options||{};
//...
};
Hipster Hack #4 - Guard
Operator &&
Use the && operator to eliminate almost all IF calls and make your code
nicers
//Boring
if(isThisAwesome){
alert('yes');//it'snot
}
//Awesome
isThisAwesome&&alert('yes');
//Alsocoolforguardingyourcode
varaCoolFunction=undefined;
aCoolFunction&&aCoolFunction();//won'trunnorcrash
Hipster Hack #5 - XXX Operator
Whenever I write some code, but then have to consult the web, or a different part of the code, I add an xxx line to the code.
This makes the code break so I can get back to the specific place and fix it later. Much easier to search for it (xxx usually
never appears) and you don’t have to think about a TODO comment.
varz=15;
doSomeMath(z,10);
xxx//Greatplaceholder.I'mtheonlyoneusingxxxandit'ssoeasytofindincodeinsteadofT
doSomeMoreMath(z,15);
Hipster Hack #6 - Timing
Use the console’s timing methods to test for event-loop blocking code
vara=[1,2,3,4,5,6,7,8,9,10];
console.time('testing_forward');
for(vari=0;i<a.length;i++);
console.timeEnd('testing_forward');
//output:testing_forward:0.041ms
console.time('testing_backwards');
for(vari=a.length-1;i>=0;i--);
console.timeEnd('testing_backwards');
//output:testing_backwards:0.030ms
Hipster Hack #7 - Debugging
Drop a debugger statement and the debugger will stop on that line
varx=1;
debugger;//Codeexecutionstopshere,happydebugging
x++;
varx=Math.random(2);
if(x>0.5){
debugger;//Conditionalbreakpoint
}
x--;
More...
There are 2 more hipster hacks in the full blog post:
Javascript Hacks for Hipsters
Happy Coding
Tal Bereznitskey
Blog: berzniz.com
Twitter: @ketacode

More Related Content

What's hot

Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
Kang-min Liu
 
ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015
qmmr
 

What's hot (20)

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
 
There's a Filter For That
There's a Filter For ThatThere's a Filter For That
There's a Filter For That
 
Class 7a: Functions
Class 7a: FunctionsClass 7a: Functions
Class 7a: Functions
 
Ruby and Rails by example
Ruby and Rails by exampleRuby and Rails by example
Ruby and Rails by example
 
Advanced Shell Scripting
Advanced Shell ScriptingAdvanced Shell Scripting
Advanced Shell Scripting
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
De 0 a 100 con Bash Shell Scripting y AWK
De 0 a 100 con Bash Shell Scripting y AWKDe 0 a 100 con Bash Shell Scripting y AWK
De 0 a 100 con Bash Shell Scripting y AWK
 
Ruby - Uma Introdução
Ruby - Uma IntroduçãoRuby - Uma Introdução
Ruby - Uma Introdução
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
Vim Hacks
Vim HacksVim Hacks
Vim Hacks
 
Fancy talk
Fancy talkFancy talk
Fancy talk
 
Introduction to CoffeeScript
Introduction to CoffeeScriptIntroduction to CoffeeScript
Introduction to CoffeeScript
 
Ruby Programming Language
Ruby Programming LanguageRuby Programming Language
Ruby Programming Language
 
Php radomize
Php radomizePhp radomize
Php radomize
 
Background Processing in Ruby on Rails
Background Processing in Ruby on RailsBackground Processing in Ruby on Rails
Background Processing in Ruby on Rails
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 
ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015
 
JavaScript Design Patterns
JavaScript Design PatternsJavaScript Design Patterns
JavaScript Design Patterns
 
파이썬 플라스크로 배우는 웹프로그래밍 #2 (ABCD)
파이썬 플라스크로 배우는 웹프로그래밍 #2 (ABCD)파이썬 플라스크로 배우는 웹프로그래밍 #2 (ABCD)
파이썬 플라스크로 배우는 웹프로그래밍 #2 (ABCD)
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
 

Viewers also liked

IT Project Tracking and Prioritization - Kickoff
IT Project Tracking and Prioritization - KickoffIT Project Tracking and Prioritization - Kickoff
IT Project Tracking and Prioritization - Kickoff
Margaret D.
 
Lyn pugecte bereket folder
Lyn pugecte  bereket folderLyn pugecte  bereket folder
Lyn pugecte bereket folder
breket
 

Viewers also liked (19)

Making your Angular.js Application accessible
Making your Angular.js Application accessibleMaking your Angular.js Application accessible
Making your Angular.js Application accessible
 
AEJMC 2014 - How scientists see engagement goals
AEJMC 2014 - How scientists see engagement goalsAEJMC 2014 - How scientists see engagement goals
AEJMC 2014 - How scientists see engagement goals
 
Smart goals
Smart goalsSmart goals
Smart goals
 
Goal setting and prioritizing
Goal setting and prioritizingGoal setting and prioritizing
Goal setting and prioritizing
 
Gartner Report: Aligning Supply and Demand for IT Services
Gartner Report: Aligning Supply and Demand for IT ServicesGartner Report: Aligning Supply and Demand for IT Services
Gartner Report: Aligning Supply and Demand for IT Services
 
CCI Project Prioritization Project
CCI Project Prioritization ProjectCCI Project Prioritization Project
CCI Project Prioritization Project
 
Smart Goals Presentation
Smart Goals PresentationSmart Goals Presentation
Smart Goals Presentation
 
Project Prioritization Tool
Project Prioritization ToolProject Prioritization Tool
Project Prioritization Tool
 
Prioritising almost anything – UXcamp London
Prioritising almost anything – UXcamp LondonPrioritising almost anything – UXcamp London
Prioritising almost anything – UXcamp London
 
IT Project Tracking and Prioritization - Kickoff
IT Project Tracking and Prioritization - KickoffIT Project Tracking and Prioritization - Kickoff
IT Project Tracking and Prioritization - Kickoff
 
Easy Project Prioritization
Easy Project PrioritizationEasy Project Prioritization
Easy Project Prioritization
 
The Javascript Toolkit 2.0
The Javascript Toolkit 2.0The Javascript Toolkit 2.0
The Javascript Toolkit 2.0
 
Roadmaps That Inspire
Roadmaps That InspireRoadmaps That Inspire
Roadmaps That Inspire
 
Utilizando recursos de realidade aumentada em aplicações
Utilizando recursos de realidade aumentada em aplicaçõesUtilizando recursos de realidade aumentada em aplicações
Utilizando recursos de realidade aumentada em aplicações
 
Lyn pugecte bereket folder
Lyn pugecte  bereket folderLyn pugecte  bereket folder
Lyn pugecte bereket folder
 
Audience
AudienceAudience
Audience
 
Realidade Virtual/Aumentada e Mixed Reality. Estamos no Futuro?
Realidade Virtual/Aumentada e Mixed Reality. Estamos no Futuro?Realidade Virtual/Aumentada e Mixed Reality. Estamos no Futuro?
Realidade Virtual/Aumentada e Mixed Reality. Estamos no Futuro?
 
The Host opening analysis
The Host opening analysisThe Host opening analysis
The Host opening analysis
 
Stats joueurs
Stats joueursStats joueurs
Stats joueurs
 

Recently uploaded

AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
Alluxio, Inc.
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
mbmh111980
 

Recently uploaded (20)

Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
A Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationA Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data Migration
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Workforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfWorkforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdf
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
A Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data MigrationA Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data Migration
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Benefits of Employee Monitoring Software
Benefits of  Employee Monitoring SoftwareBenefits of  Employee Monitoring Software
Benefits of Employee Monitoring Software
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
KLARNA - Language Models and Knowledge Graphs: A Systems Approach
KLARNA -  Language Models and Knowledge Graphs: A Systems ApproachKLARNA -  Language Models and Knowledge Graphs: A Systems Approach
KLARNA - Language Models and Knowledge Graphs: A Systems Approach
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
INGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignINGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by Design
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
 

JavaScript Hacks for Hipsters