Some Amazing operations of JavaScript arrays and javascript Arrays

Source: Internet
Author: User
Tags javascript array

Some Amazing operations of JavaScript arrays and javascript Arrays

In programming languages, the importance of arrays is self-evident. arrays in JavaScript are also one of the most commonly used objects. arrays are ordered sets of values. Due to the weak type, arrays in JavaScript are flexible and powerful. arrays of high-level languages such as Java can only store the same type or its sub-type elements. JavaScript can store multiple types of elements in the same array, the length can also be dynamically adjusted. The array length can be automatically changed as the data increases or decreases.

Today, I reviewed the JavaScript array and summarized some of his wonderful behaviors. Here I will share it with you. If something is wrong, please point it out!

Wonderful 1: The Array () constructor function can be called without the new Keyword:

The Array () constructor uses the parameter passed to it as an Array element to create an Array. Generally, we call the following:

var a = new Array(1, 2, "bom!");a.length; //3console.log(a); //[1, 2, "bom!"] 

However, it is also possible to omit new, as shown below:

var a = Array(1, 2, "bom!");a.length; //3console.log(a); //[1, 2, "bom!"] 

Although I don't know what its internal implementation mechanism is, I guess its constructor function may be defined as follows:

Function Array (args) {// if this is not an Array instance, // It indicates that it is not called through new, then call if (! This instanceof Array) {return new Array (args);} // The implementation code for normal calls.
//...
}

Odd 2: unpredictable behavior when only one parameter is passed to the constructor

If only one parameter is passed and this parameter is an integer, an array is obtained and length is equal to this parameter.

var a = new Array(12);console.log(a.length); //12console.log(a); //[] 

If only one floating point number is transferred, an error is returned:

var a = new Array(1.1); //Uncaught RangeError: Invalid array length(…) 

Passing a string will work normally, and the string serves as the first element of the array:

var a = new Array("1.1");console.log(a.length); //console.log(a); //["1.1"] 

However, to avoid ambiguity, we recommend that you create an array directly in the literal form:

Var a = []; // empty array var a = [1, 1, "bom"]; // three elements var a = [12]; // one element, and the element is 12

Wonderful 3: The length attribute of the array can be modified (writable)

As shown in the following figure, we changed the length of originally 2 to 100, and the modification was successful !!!

var a = [1, 2, 3, 4];console.log(a.length); //4a.length = 100; console.log(a.length); //100 

Although length is equal to 100, element a [4]-a [99] does not exist, and if you request their values, for example, from 0 to. if length is done in a loop, undefined is obtained.

Next, let's look at it again:

var a = [1, 2, 3, 4];a.length = 100;console.log(a[10]); //undefinedconsole.log(99 in a); //false 

Similar to the following example:

var a = [1, 2, 3, 4];a[99] = undefined;console.log(a.length); //100 

The difference is that a [99] exists, because we create it, even if it has undefined values. However, all elements from a [4] to a [98] do not exist, as shown below:

var a = [1, 2, 3, 4];a[99] = undefined;console.log(99 in a); //true;console.log(98 in a); //falseconsole.log(a.length); //100 

I have shared some of the wonderful operations of JavaScript arrays. Sorry for the poor writing of this Article. Thank you!

Articles you may be interested in:
  • Mutual conversion between js arrays and strings
  • Js uses the for loop to query whether a value exists in the array.
  • Tips for inserting an element at a specified position in a specific index of a JS Array
  • JavaScript array functions: unshift, shift, pop, and push instances
  • N methods for merging arrays in JavaScript
  • Use different methods to combine/merge two JS Arrays
  • Comparison of several methods and advantages of JS array Merging
  • Discuss the performance of js String Array concatenation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.