Home > Web Front-end > JS Tutorial > Arrays in JavaScript

Arrays in JavaScript

Linda Hamilton
Release: 2025-01-19 02:29:14
Original
204 people have browsed it

Arrays in JavaScript

Detailed explanation of core JavaScript array methods

The following are some commonly used JavaScript array methods:

  1. push(): Adds one or more elements to the end of the array and returns the length of the new array.

    <code class="language-javascript">let numbers = [1, 2, 3];
    console.log(numbers.push(4)); // 输出:4  numbers数组变为 [1, 2, 3, 4]</code>
    Copy after login
  2. slice(): Extract elements within the specified index range from the array and return a new array. The original array is not changed.

    <code class="language-javascript">let numbers = [1, 2, 3, 4, 5];
    console.log(numbers.slice(1, 4)); // 输出:[2, 3, 4]  (从索引1到3)</code>
    Copy after login
  3. splice(): Add or remove elements from the array. You can specify the starting index, the number of elements to delete, and the new elements to insert.

    <code class="language-javascript">let fruits = ['apple', 'mango', 'orange', 'pear'];
    fruits.splice(2, 0, 'pawpaw', 'strawberries'); // 从索引2开始,不删除任何元素,插入'pawpaw'和'strawberries'
    console.log(fruits); // 输出:['apple', 'mango', 'pawpaw', 'strawberries', 'orange', 'pear']
    
    fruits.splice(2, 1); // 从索引2开始,删除1个元素
    console.log(fruits); // 输出:['apple', 'mango', 'strawberries', 'orange', 'pear']</code>
    Copy after login
  4. concat(): Concatenates two or more arrays and returns a new array. The original array is not changed.

    <code class="language-javascript">let arr1 = [1, 2, 3, 4];
    let arr2 = [5, 6, 7];
    console.log(arr1.concat(arr2)); // 输出:[1, 2, 3, 4, 5, 6, 7]
    let arr3 = [8, 9];
    console.log(arr1.concat(arr2, arr3)); // 输出:[1, 2, 3, 4, 5, 6, 7, 8, 9]</code>
    Copy after login
  5. fill(): Replaces elements in the specified range in the array with the specified value.

    <code class="language-javascript">let arr4 = [1, 2, 3, 4];
    arr4.fill('Anurag', 2, 4); // 从索引2到3,用'Anurag'填充
    console.log(arr4); // 输出:[1, 2, 'Anurag', 'Anurag']</code>
    Copy after login
  6. shift(): Delete the first element of the array and return that element.

    <code class="language-javascript">let arr4 = [1, 2, 3, 4];
    arr4.shift();
    console.log(arr4); // 输出:[2, 3, 4]</code>
    Copy after login
  7. indexOf(): Returns the index of the first occurrence of the specified element in the array. If the element does not exist, returns -1.

    <code class="language-javascript">let arr4 = [1, 2, 3, 4];
    console.log(arr4.indexOf(3)); // 输出:2</code>
    Copy after login
  8. lastIndexOf(): Returns the index of the last occurrence of the specified element in the array. If the element does not exist, returns -1.

    <code class="language-javascript">let arr4 = [1, 2, 3, 4, 3];
    console.log(arr4.lastIndexOf(3)); // 输出:4</code>
    Copy after login
  9. includes(): Determines whether the array contains the specified element and returns a Boolean value.

    <code class="language-javascript">let arr4 = [1, 2, 3, 4];
    console.log(arr4.includes(4)); // 输出:true
    console.log(arr4.includes(5)); // 输出:false</code>
    Copy after login
  10. pop(): Delete the last element of the array and return that element.

    <code class="language-javascript"> let arr4 = [1, 2, 3, 4];
     arr4.pop();
     console.log(arr4); // 输出:[1, 2, 3]</code>
    Copy after login
  11. join(): Concatenates array elements into a string and returns the string. Delimiters can be specified.

    <code class="language-javascript"> let arr4 = [1, 2, 3];
     console.log(arr4.join('and')); // 输出:1and2and3</code>
    Copy after login
  12. unshift(): Adds one or more elements to the beginning of the array and returns the length of the new array.

    <code class="language-javascript"> let arr4 = [1, 2, 3];
     arr4.unshift(0);
     console.log(arr4); // 输出:[0, 1, 2, 3]</code>
    Copy after login

My GitHub link
My LinkedIn link (please replace with your actual link)

The above is the detailed content of Arrays in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template