Overview of js basic arrays, array length, array traversal
Brief introduction to the array in js, and getting the length of the js array, traversing the array in js, let us learn js Array!
1. Overview of Arrays An array represents a collection of ordered data.
Representation method of array: []. You can think that as long as you see square brackets, it is an array.
Each data in the array is separated by commas, and there is no comma in the last item.
array: Array
1 // 定义一个数组 2 var arr = [1,2,4,5]; 3 console.log(arr[2]);
Use index (index), also called subscript, to accurately read and set an item in the array. Array subscripts start from 0.
1 arr[index]; //读取 设置:arr[2] = 456;
Each item in the array can be a different data type. Usually we are used to writing the same data type as a set of arrays.
2. The length of the array
The data of the array is referenced to the data type.
1 // 数组的数据类型 2 console.log(typeof arr);
Reference types generally have their own properties and methods ;
Properties: characteristics of things. For example: a person's height, shortness, fatness or thinness.
Method: It is the behavior of things. For example: playing computer, sleeping.
We can use the dot method to call attributes.
1 Reference data. Attribute
The length of the array is represented by the length attribute. Call directly.
1 // 得到数组的长度 2 console.log(arr.length);
The last item of the array: the subscript is the array length minus 1.
1 arr[arr.length - 1];
If the subscript exceeds arr.length - 1, the value does not exist , the output undefined is undefined.
1 var arr = [1,23,45,2,56,1,67,89]; 2 // 数组的最大下标是length -1,当超过这个下标时输出undefined 3 console.log(arr[9]);
We can assign a value to an item through a subscript. If we assign a value to an item whose subscript exceeds arr.length - 1, the assignment is forced and the length of the array is forcibly lengthened.
1 var arr = [1,23,45,2,56,1,67,89]; 2 arr[20] = 20; 3 console.log(arr[20]); 4 // 现在的数组长度是多少 5 console.log(arr.length);
Although the length has been lengthened, the items that are not assigned a value in the middle are still undefined.
arr.length can also be forced to assign a value. If it exceeds the original length, the extra part is undefined if it is not assigned a value. If it is less than the original length, the extra subscript data will be deleted directly.
1 var arr1 = [1,2,45,67,2,5566,67,12]; 2 console.log(arr1.length); 3 arr1.length = 5; 4 console.log(arr1.length); 5 console.log(arr1);
3. Array traversalWe can get each item in the array through a certain method. This is traversal.
We traverse the array through for loop.
1 var arr = [1,2,"号","",function(){},12,23,null,false]; 2 // 遍历输出数组中每一项 3 for(var i = 0 ; i <= arr.length - 1 ; i ++){ 4 console.log(arr); 5 }
Case: arr = [2,4,6,7,8]; Find the factorial of each item in the array and then sum it.
1 // 案例 2 var arr = [2,4,6,7,8]; 3 // 求阶乘和 4 // 累加器 5 var sum = 0; 6 for(var i = 0 ; i <= arr.length - 1 ; i ++){ 7 // 需要一个求阶乘的函数 8 sum += jiecheng(arr); 9 } 10 console.log(sum); 11 12 13 // 阶乘函数 14 // 累乘器 15 function jiecheng(a){ 16 var cheng = 1; 17 for(var i = 1 ; i <= a ; i ++){ 18 cheng *= i; 19 } 20 return cheng; 21 }
The above is an overview of js basic arrays, the length of arrays, and all the traversal of arrays. It briefly describes how to obtain the length and traverse of js arrays
Related recommendations:
Summary of common JS error types
5 practical JS functions that can be used directly Code snippet
Native js implements movable prompt div box source code
The above is the detailed content of Overview of js basic arrays, array length, array traversal. For more information, please follow other related articles on the PHP Chinese website!

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

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.


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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.
