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

KEY
Your Library Sucks, and why you should use it.
ZIP
Yes, But
PDF
Lettering js
PPTX
ES6 and BEYOND
PDF
Orlando BarCamp Why Javascript Doesn't Suck
PDF
Real life-coffeescript
PDF
Descobrindo a linguagem Perl
PDF
2013-06-24 - Software Craftsmanship with JavaScript
Your Library Sucks, and why you should use it.
Yes, But
Lettering js
ES6 and BEYOND
Orlando BarCamp Why Javascript Doesn't Suck
Real life-coffeescript
Descobrindo a linguagem Perl
2013-06-24 - Software Craftsmanship with JavaScript

What's hot (20)

PDF
2013-06-15 - Software Craftsmanship mit JavaScript
PDF
There's a Filter For That
PDF
Class 7a: Functions
PDF
Ruby and Rails by example
KEY
Advanced Shell Scripting
PDF
Good Evils In Perl
PDF
De 0 a 100 con Bash Shell Scripting y AWK
PDF
Ruby - Uma Introdução
KEY
An introduction to Ruby
PDF
Vim Hacks
PDF
PDF
Introduction to CoffeeScript
PDF
Ruby Programming Language
PDF
Php radomize
PDF
Background Processing in Ruby on Rails
PDF
Ruby on Rails Presentation
PDF
ECMAScript2015
PDF
JavaScript Design Patterns
PDF
파이썬 플라스크로 배우는 웹프로그래밍 #2 (ABCD)
PDF
Introdução ao Perl 6
2013-06-15 - Software Craftsmanship mit JavaScript
There's a Filter For That
Class 7a: Functions
Ruby and Rails by example
Advanced Shell Scripting
Good Evils In Perl
De 0 a 100 con Bash Shell Scripting y AWK
Ruby - Uma Introdução
An introduction to Ruby
Vim Hacks
Introduction to CoffeeScript
Ruby Programming Language
Php radomize
Background Processing in Ruby on Rails
Ruby on Rails Presentation
ECMAScript2015
JavaScript Design Patterns
파이썬 플라스크로 배우는 웹프로그래밍 #2 (ABCD)
Introdução ao Perl 6

Viewers also liked (19)

PDF
Making your Angular.js Application accessible
PPTX
AEJMC 2014 - How scientists see engagement goals
PPTX
Smart goals
PPTX
Goal setting and prioritizing
PDF
Gartner Report: Aligning Supply and Demand for IT Services
PDF
CCI Project Prioritization Project
PPT
Smart Goals Presentation
PDF
Project Prioritization Tool
PDF
Prioritising almost anything – UXcamp London
PPT
IT Project Tracking and Prioritization - Kickoff
PPTX
Easy Project Prioritization
PDF
The Javascript Toolkit 2.0
PPTX
Roadmaps That Inspire
PPTX
Utilizando recursos de realidade aumentada em aplicações
PPTX
Lyn pugecte bereket folder
PPT
Audience
PPTX
Realidade Virtual/Aumentada e Mixed Reality. Estamos no Futuro?
PPTX
The Host opening analysis
ODS
Stats joueurs
Making your Angular.js Application accessible
AEJMC 2014 - How scientists see engagement goals
Smart goals
Goal setting and prioritizing
Gartner Report: Aligning Supply and Demand for IT Services
CCI Project Prioritization Project
Smart Goals Presentation
Project Prioritization Tool
Prioritising almost anything – UXcamp London
IT Project Tracking and Prioritization - Kickoff
Easy Project Prioritization
The Javascript Toolkit 2.0
Roadmaps That Inspire
Utilizando recursos de realidade aumentada em aplicações
Lyn pugecte bereket folder
Audience
Realidade Virtual/Aumentada e Mixed Reality. Estamos no Futuro?
The Host opening analysis
Stats joueurs

Recently uploaded (20)

PDF
top salesforce developer skills in 2025.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
L1 - Introduction to python Backend.pptx
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
AI in Product Development-omnex systems
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Introduction to Artificial Intelligence
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
ai tools demonstartion for schools and inter college
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Digital Strategies for Manufacturing Companies
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Design an Analysis of Algorithms II-SECS-1021-03
top salesforce developer skills in 2025.pdf
Odoo POS Development Services by CandidRoot Solutions
L1 - Introduction to python Backend.pptx
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
AI in Product Development-omnex systems
VVF-Customer-Presentation2025-Ver1.9.pptx
Odoo Companies in India – Driving Business Transformation.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Understanding Forklifts - TECH EHS Solution
Introduction to Artificial Intelligence
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
ai tools demonstartion for schools and inter college
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Navsoft: AI-Powered Business Solutions & Custom Software Development
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Digital Strategies for Manufacturing Companies
wealthsignaloriginal-com-DS-text-... (1).pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Design an Analysis of Algorithms II-SECS-1021-03

JavaScript Hacks for Hipsters