Javascript Array Objects - Properties and Methods
Description
An Array is used to store a number of values (called as elements) in order with a single variable.
Can be created using javascript array object constructor.
new Array(size)
new Array(element0, element1, ..., elementN)
Parameters
size: The length of the array. If the value specified is not a number, an array of length 1 is created, with the first element having the specified value. The maximum length allowed for an array is 4,294,967,295.
element0,element1,...., elementN: A list of values for the array's elements
Example
w3r = new array ("php","html","css"); creates an array.
w3r[0] refers to php,
w3r[1] refers to html and
w3r[2] refers to css.
Javascript Array Objects Property
Name | Description | Version |
---|---|---|
constructor | Specifies the function which creates an object's prototype. | Implemented in JavaScript 1.2 |
index | The position of a regular expression match within a string. | Implemented in JavaScript 1.2 |
input | Contains the original string against which the regular expression was matched. | Implemented in JavaScript 1.2 |
length | Returns the number of elements in an array | Implemented in JavaScript 1.2 |
prototype | Use to add new properties to all objects. | Implemented in JavaScript 1.2 |
Javascript Array Objects Methods
Name | Description | Version |
---|---|---|
concat | Use to join two or more arrays and returns a new array. | Implemented in JavaScript 1.2 |
join | Use to join all elements of an array into a string. | Implemented in JavaScript 1.2 |
pop | Use to remove the last element from an array. | Implemented in JavaScript 1.2 |
push | Use to add one or more elements at the end of an array. | Implemented in JavaScript 1.2 |
reverse | Use to reverse the order of the elements in an array. | Implemented in JavaScript 1.2 |
shift | Use to remove first element from an array. | Implemented in JavaScript 1.2 |
slice | Use to extract a section of an array. | Implemented in JavaScript 1.2 |
splice | Use to remove old elements and add new elements to an array. | Implemented in JavaScript 1.2 |
sort | Use to sort the elements of an array. | Implemented in JavaScript 1.2 |
tosource | Returns a string which represent the source code of the array. | Implemented in JavaScript 1.2 |
tostring | Returns a string represent the array and its elements. | Implemented in JavaScript 1.2 |
unshift | Use to add one or more elements to the beginning of an array and returns the new length of the array. | Implemented in JavaScript 1.2 |
valueof | Returns the primitive value of an array. | Implemented in JavaScript 1.2 |
See also :
JavaScript Core objects, methods, properties.
Previous: JavaScript: Core Objects
Next:
JavaScript constructor Property: Array Object
Test your Programming skills with w3resource's quiz.
JavaScript: Tips of the Day
How to insert an item into an array at a specific index (JavaScript)?
What you want is the splice function on the native array object.
arr.splice(index, 0, item); will insert item into arr at the specified index (deleting 0 items first, that is, it's just an insert). In this example we will create an array and add an element to it into index 2:
var arr = []; arr[0] = "Jani"; arr[1] = "Hege"; arr[2] = "Stale"; arr[3] = "Kai Jim"; arr[4] = "Borge"; console.log(arr.join()); arr.splice(2, 0, "Lene"); console.log(arr.join());
Ref: https://bit.ly/2BXbp04
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework