
Learning JavaScript Data Structures and Algorithms
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
- [*] Master existing JavaScript data structures such as array, set and map and learn how to implement new ones such as stacks, linked lists, trees and graphs.
- [*] All concepts are explained in an easy way, followed by examples.
Book DescriptionThis book begins by covering basics of the JavaScript language and introducing ECMAScript 7, before gradually moving on to the current implementations of ECMAScript 6. You will gain an in-depth knowledge of how hash tables and set data structure functions, as well as how trees and hash maps can be used to search files in a HD or represent a database. This book is an accessible route deeper into JavaScript. Graphs being one of the most complex data structures you'll encounter, we'll also give you a better understanding of why and how graphs are largely used in GPS navigation systems in social networks. Toward the end of the book, you'll discover how all the theories presented by this book can be applied in real-world solutions while working on your own computer networks and Facebook searches. What you will learn - [*] Declare, initialize, add, and remove items from arrays, stacks, and queues
- [*] Get the knack of using algorithms such as DFS (Depth-first Search) and BFS (Breadth-First Search) for the most complex data structures
- [*] Harness the power of creating linked lists, doubly linked lists, and circular linked lists
- [*] Store unique elements with hash tables, dictionaries, and sets
- [*] Use binary trees and binary search trees
- [*] Sort data structures using a range of algorithms such as bubble sort, insertion sort, and quick sort
Who this book is forIf you are a student of Computer Science or are at the start of your technology career and want to explore JavaScript's optimum ability, this book is for you. You need a basic knowledge of JavaScript and programming logic to start having fun with algorithms.
All prices
More details
Other editions
New editions

Additional editions

Content
- Arrays
- Stacks
- Queues
- Linked Lists
- Sets
- Dictionaries and Hashes
- Trees
- Graphs
- Sorting and Searching Algorithms
- Patterns of Algorithm
- Algorithm Complexity
JavaScript basics
Before we start diving into the various data structures and algorithms, let's have a quick overview of the JavaScript language. This section will present the JavaScript basics required to implement the algorithms we will create in the subsequent chapters.
To start, let's take a look at the two different ways we can use JavaScript code on an HTML page:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <script> alert('Hello, World!'); </script> </body> </html>The first way is demonstrated by the previous code. We need to create an HTML file and write this code on it. In this example, we are declaring the script tag inside the HTML file and, inside the script tag, we have the JavaScript code.
For the second example, we need to create a JavaScript file (we can save it as 01-HelloWorld.js) and, inside this file, we will insert the following code:
Then, our HTML file will look similar to this:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <script src="01-HelloWorld.js"> </script> </body> </html>The second example demonstrates how to include a JavaScript file inside an HTML file.
By executing any of these two examples, the output will be the same. However, the second example is best practice.
Note
You may find JavaScript include statements or JavaScript code inside the head tag in some examples on the Internet. As best practice, we will include any JavaScript code at the end of the body tag. This way, the HTML will be parsed by the browser and displayed before the scripts are loaded. This boosts the performance of the page.
Variables
Variables store data that can be set, updated, and retrieved whenever needed. Values that are assigned to a variable belong to a type. In JavaScript, the available types are numbers, strings, Booleans, functions, and objects. We also have undefined and null, along with arrays, dates, and regular expressions.
Note
Although JavaScript has different available variable types, it is not a strongly typed language such as C/C++, C#, Java. In strongly typed languages, we need to declare the type of the variable along with its declaration (in Java, for example, to declare an integer variable, we use int num = 1;). In JavaScript, we only need to use the keyword var, and we do not need to declare the variable type. For this reason, JavaScript is not a strongly typed language.
The following is an example of how to use variables in JavaScript:
var num = 1; //{1} num = 3; //{2} var price = 1.5; //{3} var name = 'Packt'; //{4} var trueValue = true; //{5} var nullVar = null; //{6} var und; //{7}- On line
{1}, we have an example of how to declare a variable in JavaScript (we are declaring a number). Although it is not necessary to use thevarkeyword declaration, it is good practice to always specify when we declare a new variable. - On line
{2}, we updated an existing variable. JavaScript is not a strongly typed language. This means you can declare a variable, initialize it with a number, and then update it with a string or any other datatype. Assigning a value to a variable that is different from its original type is also not good practice. - On line
{3}, we also declared a number, but this time it is a decimal floating point. On line{4}, we declared a string; on line{5}, we declared a Boolean. On line{6}, we declared anullvalue, and on line{7}, we declared an undefined variable. Anullvalue means no value, andundefinedmeans a variable that has been declared but not yet assigned a value. Take a look at the following:
If we want to see the value of each variable we declared, we can use console.log to do so, as listed in the previous code snippet.
Note
We have three ways of outputting values in JavaScript that we can use with the examples of this book. The first one is alert('My text here'), which outputs an alert window on the browser, and the second one is console.log('My text here'), which outputs text on the Console tab of the debug tool (Google Developer Tools or Firebug, depending on the browser you are using). The third way is outputting the value directly on the HTML page that is rendered by the browser using document.write('My text here'). You can use the option that you feel most comfortable with.
The console.log method also accepts more than just arguments. Instead of console.log("num: "+ num), we can also use console.log("num: ", num).
We will discuss functions and objects later in this chapter.
Variable scope
Scope refers to where in the algorithm we can access the variable (it can also be a function when we work with function scopes). There are local and global variables.
Let's look at an example:
var myVariable = 'global'; myOtherVariable = 'global'; function myFunction(){ var myVariable = 'local'; return myVariable; } function myOtherFunction(){ myOtherVariable = 'local'; return myOtherVariable; } console.log(myVariable); //{1} console.log(myFunction()); //{2} console.log(myOtherVariable); //{3} console.log(myOtherFunction()); //{4} console.log(myOtherVariable); //{5}- Line
{1}will outputglobalbecause we are referring to aglobalvariable. - Line
{2}will outputlocalbecause we declared themyVariablevariable inside themyFunctionfunction as a local variable, so the scope will only be insidemyFunction. - Line
{3}will outputglobalbecause we are referencing the global variable namedmyOtherVariablethat was initialized on the second line of the example. - Line
{4}will outputlocal. Inside themyOtherFunctionfunction, we referencing themyOtherVariableglobal variable and assigning the valuelocalto it because we are not declaring the variable using thevarkeyword. - For this reason, line
{5}will outputlocal(because we changed the value of the variable insidemyOtherFunction).
You may hear that global variables in JavaScript are evil and this is true. Usually, the quality of JavaScript source code is measured by the number of global variables and functions (a large number is bad). So, whenever possible, try avoiding global variables.
Operators
We need operators when performing any operation in a programming language. JavaScript also has arithmetic, assignment, comparison, logical, bitwise, and unary operators, among others. Let's take a look at these:
var num = 0; // {1} num = num + 2; num = num * 3; num = num / 2; num++; num--; num += 1; // {2} num -= 2; num *= 3; num /= 2; num %= 3; console.log('num == 1 : ' + (num == 1)); // {3} console.log('num === 1 : ' + (num === 1)); console.log('num != 1 : ' + (num != 1)); console.log('num > 1 : ' + (num > 1)); console.log('num < 1 : ' + (num < 1)); console.log('num >= 1 : ' + (num >= 1)); console.log('num <= 1 : ' + (num <= 1)); console.log('true && false : ' + (true && false)); // {4} console.log('true || false : ' + (true || false)); console.log('!true : ' + (!true));On line {1}, we have the arithmetic operators. In the following table, we have the operators and their descriptions:
Arithmetic operator
Description
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulus (remainder of a division operation)
++
Increment
--
Decrement
On line {2}, we have the assignment...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: PDF
Copy-Protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (only limited: Kindle).
The file format PDF always displays a book page identically on any hardware. This makes PDF suitable for complex layouts such as those used in textbooks and reference books (images, tables, columns, footnotes). Unfortunately, on the small screens of e-readers or smartphones, PDFs are rather annoying, requiring too much scrolling.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our eBook Help page.