Latest web development tutorials

JavaScript slice () method

Array Object Reference JavaScript Array Object

Examples

Read element in the array:

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,3);

citrus output results:

Orange,Lemon

try it"

Definition and Usage

slice () method returns the selected elements from the existing array.

slice () method can extract a portion of the string and returns a new string is extracted portions.

Note: slice () method does not change the original array.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support slice ().


grammar

array.slice( start , end )

Parameter Values

参数 描述
start 必需。规定从何处开始选取。如果是负数,那么它规定从数组尾部开始算起的位置。也就是说,-1 指最后一个元素,-2 指倒数第二个元素,以此类推。
end 可选。规定从何处结束选取。该参数是数组片断结束处的数组下标。如果没有指定该参数,那么切分的数组包含从 start 到数组结束的所有元素。如果这个参数是负数,那么它规定的是从数组尾部开始算起的元素。

return value

Type description
Array It returns a new array that contains from start to end (not including the element) of arrayObject the elements.

technical details

JavaScript version: 1.2


More examples

Examples

Negative values ​​read from the array element

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var myBest = fruits.slice(-3,-1);

myBest result output:

Lemon, Apple

try it"

Examples

Interception of a string

var str = "www.w3big.com!";
document.write (str.slice (4) + "<br>"); // The first five characters from the start to the end of the interception
document.write (str.slice (4,10)); // start from the first five characters in the interception of the first 10 characters

try it"

Array Object Reference JavaScript Array Object