Fork me on GitHub

JavaScript

The Right Way

Hey, you!

This is a guide intended to introduce new developers to JavaScript and help experienced developers learn more about its best practices.

Despite the name, this guide doesn't necessarily mean "the only way" to do JavaScript.

We just gather all the articles, tips, and tricks from top developers and put it here. Since it comes from exceptional folks, we could say that it is "the right way", or the best way to do so.

Choose your path

GETTING STARTED

ABOUT

Created by Netscape in 1995 as an extension of HTML for Netscape Navigator 2.0, JavaScript had as its main function the manipulation of HTML documents and form validation. Before winning this name so famous nowadays, JavaScript was called Mocha. When it first shipped in beta releases, it was officially called LiveScript and finally, when it was released by Sun Microsystems, was baptized with the name by which it is known today. Because of the similar names, people confuse JavaScript with Java. Although both have the lexical structure of programming, they are not the same language. Different from C, C# and Java, JavaScript is an interpreted language. It means that it needs an "interpreter". In case of JavaScript, the interpreter is the browser.

CURRENT VERSION

The JavaScript standard is ECMAScript. As of 2012, all modern browsers fully support ECMAScript 5.1. Older browsers support at least ECMAScript 3. A 6th major revision of the standard is being worked on.

A good reference to versions, references and news about JavaScript can be found at the Mozilla Developer Network.

THE DOM

The Document Object Model (DOM) is an API for HTML and XML documents. It provides a structural representation of the document, enabling you to modify its content and visual presentation by using a scripting language such as JavaScript. See more at Mozilla Developer Network - DOM.

JS CODE STYLE

Conventions

As every language, JavaScript has many code style guides. Maybe the most used and recommended is the Google Code Style Guide for JavaScript, but we recommend you read Idiomatic.js.

Linting

Nowadays the best tool for linting your JavaScript code is JSHint. We recommend that whenever possible you verify your code style and patterns with a Lint tool.

THE GOOD PARTS

Object Oriented

JavaScript has strong object-oriented programming capabilities, even though some debates have taken place due to the differences in object-oriented JavaScript compared to other languages.

Source: Introduction to Object-Oriented JavaScript

Anonymous Functions

Anonymous functions are functions that are dynamically declared at runtime. They’re called anonymous functions because they aren’t given a name in the same way as normal functions.

Source: JavaScript anonymous functions

Functions as First-class Objects

Functions in JavaScript are first class objects. This means that JavaScript functions are just a special type of object that can do all the things that regular objects can do.

Source: Functions are first class objects in JavaScript

Loose Typing

For many front-end developers, JavaScript was their first taste of a scripting and/or interpretive language. To these developers, the concept and implications of loosely typed variables may be second nature. However, the explosive growth in demand for modern web applications has resulted in a growing number of back-end developers that have had to dip their feet into the pool of client-side technologies. Many of these developers are coming from a background of strongly typed languages, such as C# or Java, and are unfamiliar with both the freedom and the potential pitfalls involved in working with loosely typed variables.

Source: Understanding Loose Typing in JavaScript

Scoping and Hoisting

Scoping: In JavaScript, functions are our de facto scope delimiters for declaring vars, which means that usual blocks from loops and conditionals (such as if, for, while, switch and try) DON'T delimit scope, unlike most other languages. Therefore, those blocks will share the same scope as the function which contains them. This way, it might be dangerous to declare vars inside blocks as it would seem the var belongs to that block only.

Hosting: On runtime, all var and function declarations are moved to the beginning of each function (its scope) - this is known as Hoisting. Having said so, it is a good practice to declare all the vars altogether on the first line, in order to avoid false expectations with a var that got declared late but happened to hold a value before - this is a common problem for programmers coming from languages with block scope.

Source: JavaScript Scoping and Hoisting

Function Binding

Function binding is most probably the least of your concerns when beginning with JavaScript, but when you realize that you need a solution to the problem of how to keep the context of this within another function, then you might realize that what you actually need is Function.prototype.bind().

Source: Understanding JavaScript’s Function.prototype.bind

Closure Function

Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure 'remembers' the environment in which it was created in. It is a important concept to understand as it can be useful during development, like emulating private methods. It can also help to learn how to avoid common mistakes, like creating closures in loops.

Source: MDN - Closures

Strict mode

ECMAScript 5's strict mode is a way to opt in to a restricted variant of JavaScript. Strict mode isn't just a subset: it intentionally has different semantics from normal code. Browsers not supporting strict mode will run strict mode code with different behavior from browsers that do, so don't rely on strict mode without feature-testing for support for the relevant aspects of strict mode. Strict mode code and non-strict mode code can coexist, so scripts can opt into strict mode incrementally.

Source: MDN - Strict mode

Immediately-Invoked Function Expression (IIFE)

An immediately-invoked function expression is a pattern which produces a lexical scope using JavaScript's function scoping. Immediately-invoked function expressions can be used to avoid variable hoisting from within blocks, protect against polluting the global environment and simultaneously allow public access to methods while retaining privacy for variables defined within the function.

This pattern has been referred to as a self-executing anonymous function, but @cowboy (Ben Alman) introduced the term IIFE as a more semantically accurate term for the pattern.

Source: Immediately-Invoked Function Expression (IIFE)

PATTERNS

Description

While JavaScript contains design patterns that are exclusive to the language, many classical design patterns can also be implemented.

A good way to learn about these is Addy Osmani’s open source book Learning JavaScript Design Patterns, and the links below are (in the majority) based on it.

Design Patterns

Creational Design Patterns

Structural Design Patterns

Behavioral Design Patterns

MV* Patterns

There are some implementations of the traditional MVC Pattern and its variations in JavaScript.

TESTING TOOLS

Description

Various libraries and frameworks to do tests in JavaScript.

Links

Maintained by TJ Holowaychuk

Maintained by jQuery

Maintained by Pivotal Labs

Maintained by the team behind AngularJS. Mostly by Vojta Jina

Maintained by Sitepen

A JavaScript code coverage tool written in JavaScript, maintained by Krishnan Anantheswaran

A test helper to mock functions and the XHR object, maintained by Leo Balter

Frameworks

General purpose

jQuery is a fast, small, and feature-rich JavaScript library. Built by John Resig.

Built by Yahoo!, YUI is a free, open source JavaScript and CSS library for building richly interactive web applications. New development has stopped since August 29th, 2014.

Zepto is a minimalist JavaScript library for modern browsers with a largely jQuery-compatible API. If you use jQuery, you already know how to use Zepto.

Dojo is a free, open-source JavaScript toolkit for building high performance web applications. Project sponsors include IBM and SitePen.

MV*

Very popular JavaScript client-side framework, built by @jashkenas.

Built by @wycats, jQuery and Ruby on Rails core developer.

Simplify dynamic JavaScript UIs by applying the Model-View-View Model (MVVM).

Built by Google, Angular.js is like a polyfill for the future of HTML

Cappuccino is an open-source framework that makes it easy to build desktop-caliber applications that run in a web browser.

JavaScriptMVC is an open-source framework containing the best ideas in jQuery development.

Meteor is an open-source platform for building top-quality web apps in a fraction of the time, whether you're an expert developer or just getting started.

Riot is an incredibly fast, powerful yet tiny client side (MV*) library for building large scale web applications. Despite the small size all the building blocks are there: a template engine, router, event library and a strict MVP pattern to keep things organized.

CanJS is a JavaScript framework that makes developing complex applications simple and fast. Easy-to-learn, small, and unassuming of your application structure, but with modern features like custom tags and 2-way binding.

Library

Built by Facebook. React is a JavaScript library for creating user interfaces by Facebook and Instagram. Many people choose to think of React as the V in MVC.

Handlebars provides the power necessary to let you build semantic templates effectively with no frustration.

Asynchronous templates for the browser and node.js.

Game Engines


MelonJS is a free, light-weight HTML5 game engine. The engine integrates the tiled map format making level design easier.

ImpactJS is one of the more tested-and-true HTML5 game engines with the initial release all the way back at the end of 2010. It is very well maintained and updated, and has a good-sized community backing it. There exists plenty of documentation - even two books on the subject of creating games with the engine.

LimeJS is a HTML5 game framework for building fast, native-experience games for all modern touchscreens and desktop browsers.

Crafty is a game engine that dates back to late 2010. Crafty makes it really easy to get started making JavaScript games.

Cocos2d-html5 is an open-source web 2D game framework, released under MIT License. It is a HTML5 version of Cocos2d-x project. The focus for Cocos2d-html5 development is around making Cocos2d cross platforms between browsers and native application.

Phaser is based heavily on Flixel. It is maintained by Richard Davey (Photon Storm) who has been very active in the HTML5 community for years.

Goo is a 3D JavaScript gaming engine entirely built on WebGL/HTML5

LycheeJS is a JavaScript Game library that offers a complete solution for prototyping and deployment of HTML5 Canvas, WebGL or native OpenGL(ES) based games inside the Web Browser or native environments.

Turbulenz is backed by $5M in venture funding. The engine is very polished, and primarily focused on high-quality HTML5 games. There appears to be less support for mobile devices, but they will soon catch up when all mobile devices support webGL.

Quintus is an HTML5 game engine designed to be modular and lightweight, with a concise JavaScript-friendly syntax.

Kiwi.js is a fun and friendly Open Source HTML5 Game Engine. Some people call it the WordPress of HTML5 game engines

Panda.js is a HTML5 game engine for mobile and desktop with Canvas and WebGL rendering.

News

Websites

Newsletter

Reading

Articles

by Quildreen Motta

by Yehuda Katz

by Scott Allen

by Yehuda Katz

by Ben Alman

by Nico Bevacqua

by Ryan Burgess

by Kirupa Chinnathambi

by Kirupa Chinnathambi

by Kirupa Chinnathambi

by JavaScript Is Sexy

Books

by Marijn Haverbeke

by David Flanagan

by Douglas Crockford

by Stoyan Stefanov

by James Coglan

by Nicholas C. Zakas

by Nicholas C. Zakas

by Henrik Joreteg

by Stoyan Stefanov

by Dustin Diaz

by John Resig

by Nicolas Bevacqua

by Axel Rauschmayer

by Christian Johansen

by Ryan Burgess

Free E-Books

Portals

PODCASTS



SCREENCASTS

Who To Follow

PaaS Providers



HELPERS