JavaScript Part 2 gives you the ability to Live interactive cross-browser testing.

Using a function that returns another function. (This is a powerful JavaScript feature, typically called a closure.)  The function object created by a function literal contains a link to that outer context. This is called closure. This is the source of enormous expressive power.

All function parameters in JavaScript are passed by value, and there are no reference parameters. However, there are reference objects, such as arrays, which are passed to functions by reference.

JavaScript has function scope. This means that JavaScript does not have block scope, unlike many other modern programming languages. With block scope, you can declare a variable within a block of code and the variable is not accessible outside of that block.


JavaScript experts recommend using the [] operator, saying it is more efficient than calling the  Array constructor.

shallow copy vs deep copy

javascript shift function:


Jagged Arrays

peek vs pop

Priority Queues
In the course of normal queue operations, when an element is removed from a queue, that element is always the first element that was inserted into the queue. There are certain applications of queues, however, that require that elements be removed in an order other than first-in, first-out. When we need to simulate such an application, we need to create a data structure called a priority queue.

The linked list can be used in almost every situation where a one-dimensional array is used, except when you need random access to the elements of a list. When random access is required, an array is the better data structure to use.

A circularly linked list

Hashing is a common technique for storing data in such a way that the data can be inserted and retrieved very quickly. Hashing uses a data structure called a hash table. Although hash tables provide fast insertion, deletion, and retrieval, they perform poorly for operations that involve searching, such as finding the minimum and maximum values in a data set. For these operations, other data structures such as the binary search tree are more appropriate.

breadth-first search

Bubble sort in action

The Quicksort algorithm is best to use on large datasets; its performance degrades for smaller data sets.

There are two ways to search for data in a list: sequential search and binary search. A sequential search is used when the items in a list are in random order; a binary search is used when the items in a list are in sorted order. Binary search is the more efficient algorithm, but you also have to take into account the extra time it takes to sort the data set before being able to search it for a value.

The theory is that strong typing allows a compiler to detect a large class of errors at compile time. The sooner we can detect and repair errors, the less they cost us. JavaScript is a loosely typed language, so JavaScript compilers are unable to detect type errors. This can be alarming to people who are coming to JavaScript from strongly typed languages.

This is a bad thing because global variables are evil, and in JavaScript they are fundamental.

JSLint, a JavaScript parser that can analyze a JavaScript program and report on the bad parts that it contains. JSLint provides a degree of rigor that is generally lacking in JavaScript development. It can give you confidence that your programs contain only the good parts.



When a function is stored as a property of an object, we call it a method.

In many modern languages, it is recommended that variables be declared as late as possible, at the first point of use. That turns out to be bad advice for JavaScript because it lacks block scope. So instead, it is best to declare all of the variables used in a function at the top of the function body.

We can use functions and closure to make modules. A module is a function or object that presents an interface but that hides its state and implementation.

Since JavaScript’s arrays are really objects, the delete operator can be used to remove elements from an array:

delete numbers[2];
// numbers is [‘zero’, ‘one’, undefined, ‘shi’, ‘go’]

Unfortunately, that leaves a hole in the array. This is because the elements to the right of the deleted element retain their original names. What you usually want is to decrement the names of each of the elements to the right. Fortunately, JavaScript arrays have a splice method. It can do surgery on an array,

deleting some number of elements and replacing them with other elements. The first argument is an ordinal in the array. The second argument is the number of elements to delete. Any additional arguments get inserted into the array at that point:

numbers.splice(2, 1);
// numbers is [‘zero’, ‘one’, ‘shi’, ‘go’]

The property whose value is ‘shi’ has its key changed from ‘4’ to ‘3’ . Because every property after the deleted property must be removed and reinserted with a new key, this might not go quickly for large arrays.

CommonJs: synchronous require
This style uses a synchronous require method to load a dependency and return an exported interface. A module can specify exports by adding properties to the exports object or setting the value of module.exports.

AMD: asynchronous require
Asynchronous Module Definition
Other module systems (for the browser) had problems with the synchronous require (CommonJS) and introduced an asynchronous version (and a way to define modules and exporting values):

ES6 Modules
EcmaScript 6 adds some language constructs to JavaScript, which form another module system.

These global properties return a simple value; they have no properties or methods.

Regex in Javascript

Positive Lookahead

When you want to know whether a pattern is found in a string, use the test or search method; for more information (but slower execution) use the exec or match methods.

Map, Keyed Collections

The WeakMap object is a collection of key/value pairs in which the keys are objects only and the values can be arbitrary values. The object references in the keys are held weakly meaning that they are target of garbage collection (GC) if there is no other reference to the object anymore. The WeakMap API is the same as the Map API.

Set Sample

Traditionally, a set of elements has been stored in arrays in JavaScript in a lot of situations. The new Set object, however, has some advantages:

Checking whether an element exists in an collection using indexOf for arrays is slow.
Set objects let you delete elements by their value. With an array you would have to splice based on a element’s index.
The value NaN cannot be found with indexOf in array.
Set objects store unique values, you don’t have to keep track of duplicates by yourself.

The use cases of WeakSet objects are limited. They will not leak memory so it can be safe to use DOM elements as a key and mark them for tracking

Functional programming sample

Map reduce

Functional programming, no mutation.



Leave a Reply

Your email address will not be published. Required fields are marked *