Introduction to the common methods of string and array in JavaScript

Source: Internet
Author: User

String and array as JavaScript built-in objects, many of the methods, whether in the development process, or at the time of the interview have the opportunity to be asked by the interviewer, here is a frequently used method to do an introduction, these methods have a lot of practical application scenarios, So the mastery of them is still very necessary.

1. What are the common methods of array arrays?

First create an array var abc = [1,2,3,4,5,6,7,8,9];

(1) pop (); This method deletes the last item of the array and returns the deleted value.

For example: Console.log (Abc.pop ());//9; Console.log (ABC); [1,2,3,4,5,6,7,8];

(2) push (); This method adds a value to the last face of the array and returns the value.

For example: Console.log (Abc.push (10));//10; Console.log (ABC); [1,2,3,4,5,6,7,8,9,10];

(3) Shift (); This method removes the first item of the array and returns the deleted value.

For example: Console.log (Abc.shift ());//1; Console.log (ABC); [2,3,4,5,6,7,8,9];

(4) unshift (); This method adds a value before the first item in the array and returns the length of the array.

For example: Console.log (abc.unshift (0));//10; Console.log (ABC); [0,1,2,3,4,5,6,7,8,9];

(5) reverse (); Reverses the array order.

For example: Abc.reverse (); Console.log (ABC); [9,8,7,6,5,4,3,2,1];

(6) sort (); Array ordering, but in the form of strings.

For example: var ABB = [0,1,5,10,15]; Abb.sort (); Console.log (ABB); [0,1,10,15,5];

(7) concat (); The method can create a new array based on all the items in the current array.

For example: var colors = ["Red", "blue", "yellow"];  var colors2 = Colors.concat ("Black", "orange"); ["Red", "blue", "yellow", "black", "orange"];

(8) slice (); This method can be understood to intercept an array, accept 2 parameters, just fill in a parameter to intercept from the position to the last, fill in two parameters to intercept the head and tail position, but the head does not take the tail.

For example: var colors = ["Red", "blue", "yellow", "black", "orange"];

Colors.slice (1);//["Blue", "yellow", "black", "orange"];

Colors.slice (1,3);//["Blue", "yellow"];

(9) splice (); Splice is the most powerful method in the array, it uses a lot.

Delete: You can delete any number of items, just specify 2 parameters. For example splice (0,2); The first two items in the array are deleted.

Insert: You can insert any number of items to the specified location, such as: Splice (2,0, "Red", "blue") adds red and blue two items from the second position of the array.

Replace: You can delete the item at the specified location and insert any number of items, such as: Splice (2,2, "Red", "blue") removes two items from the second position of the array and adds red and blue two items.

Splice () always returns an array, an array that is deleted from the original array, and an empty array if none.

(+) indexOf (); This method is used to retrieve the position where an array of items appears, and the first occurrence is recorded only once.

For example: var ABC = [1,2,3,4,5,6,7,8,9];  Abc.indexof (5); 4;

Note: If no value is retrieved, it returns-1;

For example: var ABC = [1,2,3,4,5,6,7,8,9];  Abc.indexof (-10); -1;

Join (), convert the array to a string, and identify the way the connection is in parentheses.

For example: var abc = ["Red", "Blue", "green", "yellow"];  Abc.join ("+"); "Red+blue+green+yellow";

2, string string commonly used methods?

First create a string var abc = "HelloWorld";

(1) charAt (); The method returns the character that corresponds to the position.

For example: Console.log (Abc.charat (1)); E

(2) concat (); stitching strings;

such as var a = "Hello";  A.concat ("World"); HelloWorld

(3) slice (); to intercept a string; Accept 2 parameters, just fill in one parameter to intercept from that position to the last, fill two parameters to represent the position of the head and tail to intercept, but the head does not take the tail.

(4) substring (); to intercept a string; Accept 2 parameters, just fill in one parameter to intercept from that position to the last, fill two parameters to represent the position of the head and tail to intercept, but the head does not take the tail.

Note: The difference between slice and substring is that when their arguments are negative, slice adds the negative number to the length of the string, and substring converts the negative number to 0.

such as var a = "Hello";  A.slice ( -3) = A.slice (2); Llo

var a = "Hello";  A.substring ( -3) = a.substring (0); Hello

(5) substr (); Intercept the string, accept 2 parameters, only one parameter is intercepted from the position to the last, fill two parameters representing the starting position and length to intercept;

such as var a = "HelloWorld"; A.substr (3,7); Represents the Intercept string the third position starts to intercept 7 strings, therefore returns "Loworld";

(6) indexOf (); This method is used to retrieve where a character appears.

(7) toLocaleUpperCase (); String to uppercase.

(8) toLocaleLowerCase (); The string is turned lowercase.

(9) split (); The string is cut and placed in an array, in parentheses representing the cut's identity.

For example: var abc = "Red,blue,green,yellow";  Abc.split (","); ["Red", "Blue", "green", "yellow"];

Introduction to the common methods of string and array in JavaScript

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.