JAVASCRIPT

 Home  Computers & Internet  Web Programming JAVASCRIPT
What is Javascript?
Javascript Placement
Syntax
Reserved Words
Variables
Data Types
Escaping Characters
Concatenation
Arithmetic Operators
Assignment Operators
Comparison Operators
Boolean Operators
Conditional Operator
If Statements
Else If Statements
If Else Statements
Switch Statements
While Loops
For Loops
Do While Loops
Break Statement
Continue Statement
prompt()
alert()
Date()
Event Handlers
String Object Methods
Math Object Methods
Window Object Methods
Arrays

HOW TO SLICE ELEMENTS OF AN ARRAY INTO PIECES (NEW ARRAY) USING THE JAVACSRIPT slice() METHOD

When working with javascript arrays, it is possible to slice an array and create a new array with some of the elements of the original array. This is achieved by using the javascript slice() method.

The general format is:

Array.slice(index start, index end);

Where Array is the name of an array. This method takes two arguments, the index number to start slice from and the index number to end slice. The second argument may be a negative numer to indicate the position from the end of the array. If only a single argument is used as index number, then the array slice method will start the slice from this index number to end of the original string. Always remember that index numbers are counted from 0 onwards.

Example with both arguments supplied:

var OriginalColor= new Array("blue", "green", "red", "yellow");

var NewColor=OriginalColor.slice(1,3);

alert(NewColor); // displays green, red

Example with one argument supplied:

var OriginalColor= new Array("blue", "green", "red", "yellow");

var NewColor=OriginalColor.slice(1);

alert(NewColor); // green, red and yellow

Example with negative second argument supplied:

var OriginalColor= new Array("blue", "green", "red", "yellow");

var NewColor=OriginalColor.slice(1, -1);

alert(NewColor); // green, red

Now Read:

How To Create An Array
How To Get Array Size
How To Loop Through An Array
How To Add Elements To An Array
How To Join Array Elements
How To Reverse Element Order In An Array
How To Slice Elements Of An Array Into Pieces
How To Sequentially Sort Elements In An Array

Home | Privacy Policy | Terms Of Use | Contact Us