JavaScript data structure and algorithm reasons

As a functional programming language, it is ideal for learning about data structures and algorithms. It’s easier to learn data structures than standard languages like C, Java, or Python, and it can be fun to learn new things.

It is important to learn about data structures and algorithms. The number one reason is that data structures and algorithms can solve common problems very efficiently, which is critical to the quality of the code you write in the future (including performance; Improper data structures or algorithms can cause performance problems. Secondly, for computer science, algorithm is the most fundamental concept. Finally, if you want to work for the best IT companies (Google, Amazon, Microsoft, eBay, etc.), data structures and algorithms are a big part of the interview question.

As a best practice, we introduce JavaScript code before closing the body tag. This allows the browser to parse and display the HTML before loading the script, which improves page performance.

(The first is alert(‘My text here’), which prints to the browser’s alert window; The second is console.log(‘My text here’), which prints text to the console TAB of a debugging tool (Google Developer Tools or Firebug, depending on the browser you’re using). Write (‘My text here’) directly to the HTML page and render it in the browser. You can debug it any way you like.

Scope refers to the part of the algorithm function we write where we can access a variable (or a function, when using function scope). There are local variables and global variables.

JavaScript based

1. The variable

Variables hold data that can be set, updated, or extracted as needed. Values assigned to variables have corresponding types. JavaScript has types like numbers, strings, booleans, functions, and objects, as well as undefined and NULL, as well as arrays, dates, and regular expressions.

In strongly typed languages, you specify the type of a variable when declaring it (for example, intnum = 1; is used to declare an integer variable in Java). . If you need to type variables while writing JavaScript, you can also use TypeScript.

Although the keyword var is not required, it is best to add it every time a new variable is declared. You can declare a variable, initialize it to a value of numeric type, and then update it to a string or other type of value, but this is not a good idea.

Generally, code quality can be measured in terms of the number of global variables and functions (the more, the worse). Therefore, avoid using global variables whenever possible. B: Why? Because global variables take up more memory (with modern computer performance, this problem can be ignored, except for large objects) 2. Increased code coupling, affecting the overall, is not conducive to maintenance expansion and reuse 3. While using global variables improves performance, however, it does not help, and the performance advantage is insignificant in terms of coupling and maintainability. Post maintenance and coupling are more important.)

2. The operator

If the first condition is not met, the following condition is not judged. Both am& and & can be used as logical and operators, am& is short-circuited and, and is not short-circuited and. In addition & can be used as integer bit operator)

By standard, there are two types of data in JavaScript. ❑ Raw data types: NULL, undefined, string, number, Boolean, and Symbol[illustration]. ❑ Derived data types/objects: JavaScript objects, including functions, arrays, and regular expressions.

JavaScript also supports the DELETE operator, which deletes attributes from an object. Look at the code below. var myObj = {name: ‘John’, age: 21}; delete myObj.age; console.log(myObj); // Output object {name: “John”} (since array is itself a special object, delete can also delete arrays. Let arr = [1,2,3,4]; Delete does not change the length of the array, but returns empty (var array = [1,2,3,4]; delete array[2]; array; // empty * 1, 2, 4; Array.length, // result is 4)

3. True and false values

4. Equality operator

Different types of values can also be treated as equal when == is used

The toNumber and toPrimitive methods are internal,

The toNumber method returns the following results for different types.

The toPrimitive method returns the following results for different types.

What about the === operator? It’s much easier. If the two value types are different, the result of the comparison is false. If two values of the same type are compared, the results are judged according to the table below.

If x and y are of different types, the result is false.

Verify the results in the table with examples. First, we know that the following code prints true (string length greater than 1). console.log(‘packt’ ? true : false); What is the result of the following line of code? console.log(‘packt’ == true); The output is false. Why is that? ❑ First, the Boolean value is converted to a number by the toNumber method, so packt == 1. ❑ Next, use toNumber to convert string values. Because the string contains letters, it is converted to NaN, and the expression becomes NaN == 1, resulting in false. What is the result of the following line of code? console.log(‘packt’ == false); The output is also false. Why is that? ❑ First, the Boolean value is converted to a number by the toNumber method, so packt == 0. ❑ Next, use toNumber to convert string values. Because the string contains letters, it is converted to NaN, and the expression becomes NaN == 0, resulting in false.

5. Control structure

Conditional statements support if… The else and switch. Loops support while, do… And while the for.

if… Else statements can also be replaced with ternary operators, such as the following if… The else statement. if (num === 1) { num–; } else { num++; } can be replaced with the ternary operator: (num === 1)? num– : num++;

Break terminates the execution of the switch statement. The absence of a break causes execution of the current case to continue until a break or switch is encountered. The default keyword executes the code in default if the expression does not match any of the previous cases (it will not execute if there is one).

var i = 0; 

 do { 

     console.log(i); 

     i++; 

 } while (i < 10);

The difference is that in a while loop, the condition is evaluated before executing the code in the loop body, while in a do… In a while loop, we execute the code in the body of the loop before we judge the condition. do… A while loop causes the code in the body of the loop to execute at least once.

var i = 0; 

 while (i < 10)

 { 

     console.log(i);

     i++; 

 }

Function of 6.

A little bit, got it.

7.JavaScript object-oriented programming

There are two ways to create a normal object. Var obj = new Object(); Var obj = {}; We can also create a complete object like this: obj = {name: {first: ‘Gandalf’, last: ‘the Grey’}, address: ‘Middle Earth’};

There are two ways to create a normal object. Var obj = new Object(); Var obj = {}; We can also create a complete object like this: obj = {name: {first: ‘Gandalf’, last: ‘the Grey’}, address: ‘Middle Earth’};

When you declare a JavaScript object, the keys in a key-value pair are the properties of the object, and the values are the values of the corresponding properties. In this book, all the classes we create, such as Stack, Set, LinkedList, Dictionary, Tree, Graph, and so on, are JavaScript objects.

In OOP, an object is an instance of a class. A class defines the characteristics of an object. We’re going to create a bunch of classes to represent algorithms and data structures. For example, we declare a class (constructor) to represent books as follows.

 function Book(title, pages, isbn) { 

 this.title = title; this.pages = pages; this.isbn = isbn; 

 } 

Instantiate this class with the following code.

 var book = new Book(‘title’, ‘pag’, ‘isbn’); 

We can then access and modify the properties of the object. console.log(book.title); // Print the name of the book

book.title = ‘new title’; // Change the title of the book

console.log(book.title); // Outputs a new title

Classes can contain functions (also commonly called methods). Functions/methods can be declared and used, as shown below. Book.prototype.printTitle = function() { console.log(this.title); };

book.printTitle(); 

We can also declare functions directly in the class definition.

 function Book(title, pages, isbn) { 

 this.title = title; this.pages = pages; this.isbn = isbn; 

 this.printIsbn = function() { console.log(this.isbn); }; } 

book.printIsbn();