search
HomeWeb Front-endJS TutorialIntroduction to array features in JavaScript_javascript skills

Unlike the Java language, arrays in JavaScript have three characteristics:

1. No type. The members of an array can be of any type, and the same array can also be composed of many members of different types.
2. The length is variable. The length of the array can be changed dynamically, so there is no out-of-bounds problem with array access in JavaScript.
3. Discontinuity. The positions of the members in the array can be continuous (0, 1, 2, 3...) or discontinuous. Any array has an attribute named length. When the array members are continuous, the length value is consistent with the number of array members; when the array members are discontinuous, the length value is greater than the number of array members. Compared with continuous arrays, the read and write performance of discontinuous arrays is worse.

Experiment:


Copy code The code is as follows:

var o = [42, "Sample Text", {x:88}];//JavaScript array is un-typed.
console.log(o);//[42, "Sample Text", Object {x=88}]
o[3] = 27;//JavaScript array is dynamic.
console.log(o);//[42, "Sample Text", Object {x=88}, 27]
o[5] = 99;//JavaScript array is sparse.
console.log(o);//[42, "Sample Text", Object {x=88}, 27, undefined, 99]


As you can see from the above example, for a discontinuous array, JavaScript will return undefined when the missing member is accessed. If the array is continuous but one of its members is undefined, the result of accessing the array is the same:


Copy code The code is as follows:

var a = [42, "Sample Text", {x:88}, 27, undefined, 99];
console.log(a);//[42, "Sample Text", Object {x=88}, 27, undefined, 99]


The array is discontinuous and has missing members, and the array is contiguous but has undefined members. In both cases, the result of accessing the array contents is the same. But there are still some subtle differences between the two, mainly in the access to array keys:


Copy code The code is as follows:

console.log(4 in o);//false
console.log(4 in a);//true


It can be seen that although the results obtained by accessing the content are the same in these two cases, their internal mechanisms are completely different: when the array is discontinuous, a certain member is missing, so when the member is accessed, JavaScript returns undefined; when the array is continuous, all members exist, but the values ​​of some members are special and are undefined.

As you can see from the above example, arrays in JavaScript are essentially just objects with numbers as keys, and are no different from ordinary key-value pairs. In fact, when reading and writing operations on the array, JavaScript will try to convert the parameter into a positive integer. If the conversion is successful, the array operation will be performed (the length property of the array is automatically updated). If it fails, the parameter will be converted into a character. After the string, read and write operations on ordinary objects are performed. Of course, in the implementation of the JavaScript interpreter, a lot of performance optimizations have been made for the feature of arrays that use numbers as keys. Therefore, in actual use, if the keys of the object are all numbers, then directly using the array object will get better results. Efficient results.

During the process of defining an array, JavaScript allows redundant commas and missing array members between two commas:


Copy code The code is as follows:

var x = [1,2,3,];//trailing comma will be omitted.
console.log(x.length);//3
                                   
var y = [1,,3];//member can be missed.
console.log(y);//[1, undefined, 3]
console.log(1 in y);//false
console.log(y.length);//3


For the creation of arrays, JavaScript supports four methods:

1. Use literals (such as the bracket expressions in the above examples) to directly create array objects.
2. Use the Array() constructor without passing in any parameters. In this case, an empty array is created, which has the same effect as [].
3. Use the Array() constructor and pass in a positive integer as the length of the array. In this case, JavaScript will reserve the corresponding memory space to store this array. It is worth noting that the keys of the array are not defined at this time, that is, there are no members in the array. The effect is the same as [,,,,]
4. Use the Array() constructor and pass in the members of the array.

Experiment:


Copy code The code is as follows:

var z = new Array(10);//pre-allocate memory, but no index is defined yet.
console.log(3 in z);//false

var m = new Array(42, 33, 99, "test", {k:99});
console.log(m);//[42, 33, 99, "test", Object {k=99}]


In the ECMAScript 5 standard, you can use Array.isArray() to determine whether an object is an array:
Copy code The code is as follows:

Array.isArray([]);//true
Array.isArray({});//false
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

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.

mPDF

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),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor