JavaScript can be used to power both big and small web applications. In order to write structured and orderly code, many JavaScript developers rely on certain design patterns.

In simple words, a design pattern in JavaScript helps make your code more readable and reusable, and it also lends a form of organizational structure to your code.

In this post, we will be taking a look at some of the most common JavaScript design patterns.

What Is A Design Pattern In JavaScript?

Design patterns are reusable pieces of code that are used in JavaScript to address similar sets of issues. On the basis of the context, problem, and implementation of the given piece of code, one or more set of design patterns might be used by developers.

As such, design patterns can be of different types, based on the manner in which they address the problem at hand. For instance, a creational design pattern might focus on the way you create objects in JavaScript, whereas a behavioral design pattern might deal with the manner in which two sets of objects interact or communicate with each other.

So, what are the major design patterns in JavaScript?

Common JavaScript Design Patterns

Theoretically speaking, there are three major JS design pattern *types*:

Creational Design Pattern

As the name suggests, Creational Design Patterns focus on the creation of object or classes. This is important in large applications where object creation needs to be controlled.

Creational Design Patterns work through the medium of classes and objects where class involves a factory method in which an instance of several derived classes are created while objects employ abstract factory methods.

Structural Design Pattern

In the Structural Design Pattern, the focus is on the object composition. Thus, this design pattern looks for ways to manage object relationships such that the application is created in a scalable way and that a change in one part of the application does not affect other parts.

Structural Design Patterns also work through the medium of classes and objects where class involves adapter which matches interfaces of different classes so that they can work together despite incompatible interfaces.

Behavioral Design Pattern

The Behavioral Design Pattern in JavaScript focuses on the communication between objects by identifying common communication patterns between them.

It is important to understand that these designs patterns are not perfect while singly implemented. However, a familiarity with each of them is a prerequisite in helping developers make educated decisions based on the needs of their application.

The aforementioned types of JS design patterns are not something that exists purely in solitude. In fact, many new patterns have evolved from the above patterns, that are being used by popular JavaScript projects. Keeping this in mind, some of the modern-day JavaScript design patterns that every web developer should know are:

The Module Design Pattern

This design pattern is the most commonly used of all JavaScript design patterns. It is focused on the public and private access to methods and variables and is commonly used to mimic classes in conventional software engineering. Further, its target is to reduce globally scoped variables in an effort to decrease the chances of collision with other code within an application. Some projects which make use of the module design pattern are jQuery, Dojo, ExtJS, and YUI.

The Module Design Pattern also has a variant: the Revealing Module Pattern. It is largely similar to the module pattern with only one contrary feature. In the Revealing Module Pattern, all the methods and variables are kept private until they are explicitly exposed, which extends a great advantage in the form of increased readability due to explicitly defined public methods and variables.

The Singleton Design Pattern

The Singleton Design Pattern is another common JavaScript patterns, and it is often used in WordPress development too. In this design pattern, the instantiation of an object is restricted to a single reference. This leads to a reduced memory footprint and thereby allows a “delayed” initialization on an as-and-when-needed basis. The advantage of this model? You can share resources without overuse. AngularJS is another popular example of this model.

The Mediator Design Pattern

The focus of the Mediator Design Pattern is on a single object which, when implemented, becomes a shared resource throughout the different pieces of an application. As such, Mediator Pattern can be used to facilitate communication across the different features of an application as well as the individual sub-pieces within one feature. You can find a practical implementation of this design pattern in this gist.


With its wide distribution and expressive format, JavaScript is one of the most popular and widely used programming languages in the world today. Moreover, with its different programming styles, JavaScript offers a great amount of flexibility such that the complexity or simplicity of the code depends solely on the developer.

However, regardless of complexity, it is the aim of every web developer to write a code that is maintainable, readable, and reusable. It is at this point that design patterns can prove useful.

Since a design pattern is nothing but a reusable software solution, it can be used to provide an organizational structure for your code, thereby making it more usable, scalable and maintainable. In other words, design patterns are established ways of programming that help facilitate maintainability, communication and performance of code.

If you are an existing JS developer, which design pattern do you employ in your apps and projects? Share your views in the comments below!

Sufyan bin Uzayr writes for various magazine and blogs, and has authored several books. He blogs about technology, Linux and open source, mobile, web design and development, typography, and Content Management Systems at Code Carbon. You can learn more about him, follow him on Twitter or friend him on Facebook and Google+.

The post A Look At Popular JavaScript Design Patterns appeared first on Torque.

Share This