Groner | Learning JavaScript Data Structures and Algorithms | E-Book | www.sack.de
E-Book

E-Book, Englisch, 314 Seiten

Groner Learning JavaScript Data Structures and Algorithms

Hone your skills by learning classic data structures and algorithms in JavaScript
2. Auflage 2024
ISBN: 978-1-78355-388-4
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)

Hone your skills by learning classic data structures and algorithms in JavaScript

E-Book, Englisch, 314 Seiten

ISBN: 978-1-78355-388-4
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)



This 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.

Groner Learning JavaScript Data Structures and Algorithms jetzt bestellen!

Autoren/Hrsg.


Weitere Infos & Material


Table of Contents - JavaScript—A Quick Overview
- 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:

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 tag inside the HTML file and, inside the tag, we have the JavaScript code.

For the second example, we need to create a JavaScript file (we can save it as ) and, inside this file, we will insert the following code:

alert('Hello, World!');

Then, our HTML file will look similar to this:

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 statements or JavaScript code inside the tag in some examples on the Internet. As best practice, we will include any JavaScript code at the end of the 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 ). In JavaScript, we only need to use the keyword , 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 , we have an example of how to declare a variable in JavaScript (we are declaring a number). Although it is not necessary to use the keyword declaration, it is good practice to always specify when we declare a new variable.
  • On line , 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 , we also declared a number, but this time it is a decimal floating point. On line , we declared a string; on line , we declared a Boolean. On line , we declared a value, and on line , we declared an undefined variable. A value means no value, and means a variable that has been declared but not yet assigned a value. Take a look at the following:
console.log("num: "+ num); console.log("name: "+ name); console.log("trueValue: "+ trueValue); console.log("price: "+ price); console.log("nullVar: "+ nullVar); console.log("und: "+ und);

If we want to see the value of each variable we declared, we can use 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 , which outputs an alert window on the browser, and the second one is , 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 . You can use the option that you feel most comfortable with.

The method also accepts more than just arguments. Instead of , we can also use .

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 will output because we are referring to a variable.
  • Line will output because we declared the variable inside the  function as a local variable, so the scope will only be inside .
  • Line will output because we are referencing the global variable named that was initialized on the second line of the example.
  • Line will output . Inside the function, we  referencing the global variable and assigning the value to it because we are not declaring the variable using the keyword.
  • For this reason, line will output (because we changed the value of the variable inside ).

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 , 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 , we have the assignment...



Ihre Fragen, Wünsche oder Anmerkungen
Vorname*
Nachname*
Ihre E-Mail-Adresse*
Kundennr.
Ihre Nachricht*
Lediglich mit * gekennzeichnete Felder sind Pflichtfelder.
Wenn Sie die im Kontaktformular eingegebenen Daten durch Klick auf den nachfolgenden Button übersenden, erklären Sie sich damit einverstanden, dass wir Ihr Angaben für die Beantwortung Ihrer Anfrage verwenden. Selbstverständlich werden Ihre Daten vertraulich behandelt und nicht an Dritte weitergegeben. Sie können der Verwendung Ihrer Daten jederzeit widersprechen. Das Datenhandling bei Sack Fachmedien erklären wir Ihnen in unserer Datenschutzerklärung.