


JavaScript study notes (5) Array array type introduction_basic knowledge
Array Creation
First type:
var colors = new Array();
var colors = new Array(20);//Create an array containing 20 items
var colors = new Array("Greg");//Create an array containing 1 item, which is a string Array of "Greg"
var colors = new Array("red","blue","green"); //Create 3 items
Second type:
var colors = ["red","blue","green"] ;
var colors = [];//Create an empty array
Note: The index of the array starts from 0
1. length attribute
length attribute The number of items in the array is saved in, such as:
var colors = ["red","blue","green"];
alert(colors.length); //3
The length attribute is not read-only, you can use the length attribute in the array Remove items at the end, or add new items, such as:
var colors = ["red","blue","green"];
colors.length = 2;
alert(colors); //red,blue
colors[colors.length] = "black";
alert(colors); //red, blue, black
2.join() method, connect the items in the array
var colors = ["red","blue","green"];
alert( colors.join(",")); //red,blue,green
alert(colors.join("||")); //red||blue||green
3. Array stack methods: push() and pop()
push() method can accept any number of parameters, add them to the end of the array one by one, and return the length of the modified array
pop() The method removes the last item from the end of the array, reduces the length value of the array, and returns the removed item
var colors = new Arrary(); //Create an array
var count = colors.push("red","green"); //Push two items to the end of the array
alert(count); //2
count = colors.push("black"); //Push an item to the end of the array
alert(count); //3
var item = colors.pop(); //Remove the last item and return the value
alert(item); //"black"
alert(count); //2
4. Array queue methods: push() and shift(), unshift()
push() method is the same as above
shift() method removes the first item in the array and returns it, and the length of the array is reduced 1 The
unshift() method adds any item to the front of the array and returns the length of the new array
var colors = new Arrary(); //Create an array
var count = colors.push("red","green"); //Push two items to the end of the array
alert(count); //2
count = colors.push("black"); //Push an item to the end of the array
alert(count); //3
var item = colors.shift(); //Remove the first item and return the value
alert(item); //"red"
alert(colors); //green,black
count = colors .unshift("blue"); //Push an item to the front of the array
alert(count); //3
alert(colors); //blue, green, black
5. Reordering methods: reverse() and sort()
reverse() method reverses the order of array items
sort() method defaults to sorting array items in ascending order by string size, and can accept a comparison size The function takes as parameter
var values = [1,2, 3,4,5];
values.reverse();
alert(values); //5,4,3,2,1
//Ascending sorting function
function compare(value1,value2) {
if (value1 return -1; //Descending order is changed to 1
} else if (value1 > value2) {
return 1; //Descending order changed to -1
} else {
return 0;
}
}
//Array sorted in ascending order
var values = [0, 1,5,15,20,10];
values.sort(compare);
alert(values);//0,1,5,10,15,20
//You can use this function for numerical types, ascending order
function compare(value1,value2) {
return value2 - value1;
}
6. Some methods of arrays: concat() method, slice() method and splice() method
The concat() method adds parameters to the end of the original array and returns a new array. The original array remains unchanged.
The slice() method returns the items in the array. If there is one parameter, it returns all items from the specified position to the end of the array. When two parameters are used, the items between the start position and the end position (excluding the end position) are returned, and the original array remains unchanged
The splice() method inserts, deletes, or replaces items in the array, and returns the deleted item (returns an empty array if not deleted), the original array changes
//concat() method
var colors = ["red","green","blue"];
var colors2 = colors.concat("yellow",["black","brown"] );
alert(colors); //red,green,blue
alert(colors2); //red,green,blue,yellow,black,brown
//slice() method
var colors = ["red"," green","blue","yellow","black"];
var colors2 = colors.slice(1); //With one parameter, return all items from the specified position to the end of the array
var colors3 = colors .slice(1,4); //If there are two parameters, return the items between the start position and the end position (excluding the end position)
alert(colors2); //green,blue,yellow,black
alert(colors3); //green,,blue,yellow
//splice() method
//Insert item, specify 3 parameters when inserting: starting position, 0 (item to be deleted), item to be inserted
var colors = ["red","green","blue"];
var inserted = colors.splice(1,0,"yellow","orange"); //Insert two items starting from position 1
alert(colors); //red,yellow,orange,green,blue
alert(inserted); //Empty array
//Replacement item, specify 3 parameters when deleting: starting position, Items to be deleted, any items to be inserted
var colors = ["red","green","blue"];
var replaced = colors.splice(1,1,"black","brown "); //Delete one item and insert two items
alert(colors); //red,black,browm,blue
alert(replaced); //green

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
