Hover over a Property or Method for an example and click to go to the MDN documentation
The length property represents an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.
var example = [1, 2, 3, 4, 5];
console.log(example.length);
Output:
5
The concat( ) method returns a new array comprised of the array on which it is called joined with the array(s) and/or value(s) provided as arguments.
var alpha = ['a', 'b', 'c'];
var numeric = [1, 2, 3];
console.log(alpha.concat(numeric));
Output:
['a', 'b', 'c', 1, 2, 3]
The join( ) method joins all elements of an array into a string.
var a = ['Wind', 'Rain', 'Fire'];
console.log(a.join(''));
console.log(a.join(', '));
Output:
['WindRainFire']
['Wind, Rain, Fire']
The map( ) method creates a new array with the results of calling a provided function on every element in this array.
var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
console.log(roots);
console.log(numbers);
Output:
[1, 2, 3]
[1, 4, 9]
The pop( ) method removes the last element from an array and returns that element.
var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
console.log(myFish);
var popped = myFish.pop();
console.log(myFish);
console.log(popped);
Output:
['angel', 'clown', 'mandarin', 'sturgeon']
['angel', 'clown', 'mandarin']
['sturgeon']
The push( ) method adds one or more elements to the end of an array and returns the new length of the array.
var sports = ['soccer', 'baseball'];
var total = sports.push('football', 'swimming');
console.log(sports);
console.log(total);
Output:
['soccer', 'baseball', 'football', 'baseball']
4
The reduce( ) method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
var total = [0, 1, 2, 3].reduce(function(a, b) {
return a + b;
});
console.log(total);
Output:
6
The reverse( ) method reverses an array in place. The first array element becomes the last and the last becomes the first.
var myArray = ['one', 'two', 'three'];
console.log(myArray.reverse());
Output:
['three', 'two', 'one']
The shift( ) method removes the first element from an array and returns that element. This method changes the length of the array.
var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
console.log('myFish before: ' + myFish);
var shifted = myFish.shift();
console.log('myFish after: ' + myFish);
console.log('Removed this element: ' + shifted);
Output:
myFish before: angel,clown,mandarin,surgeon
myFish after: clown,mandarin,surgeon
Removed this element: angel
The sort( ) method sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points.
var scores = [5, 1, 9, 4, 10];
console.log(scores.sort());
Output:
[1, 10, 4, 5, 9]
The toString( ) method returns a string representing the specified array and its elements.
var monthNames = ['Jan', 'Feb', 'Mar', 'Apr'];
console.log(monthNames.toString());
Output:
Jan,Feb,Mar,Apr
Hover over a Property or Method for an example and click to go to the MDN documentation
The length property represents the length of a string.
var example = 'This is an example';
console.log(example 'has ' + example.length + ' characters.');
Output:
This is an example has 18 characters.
The charAt( ) method returns the specified character from a string.
var anyString = 'Brave new world';
console.log("The character at index 0 is '" + anyString.charAt(0) + "'");
console.log("The character at index 999 is '" + anyString.charAt(999) + "'");
Output:
The character at index 0 is 'B'
The character at index 999 is ''
The concat( ) method combines the text of two or more strings and returns a new string.
var hello = 'Hello, ';
console.log(hello.concat('Kevin', ' have a nice day'));
Output:
Hello, Kevin have a nice day.
The includes( ) method determines whether one string may be found within another string, returning true or false as appropriate.
var str = 'To be, or not to be, that is the question.';
console.log(str.includes('To be'));
console.log(str.includes('nonexistent'));
Output:
True
False
The slice( ) method extracts a section of a string and returns a new string.
var str = 'The morning is upon us.';
console.log(str.slice(4, -2));
console.log(str.slice(0, -1));
Output:
morning is upon u
The morning is upon us
The split( ) method splits a String object into an array of strings by separating the string into substrings.
var myString = 'Hello World!';
console.log(myString.split(''));
Output:
['H','e','l','l','o',' ','W','o','r','l','d','!']
Reversing a String using split( )
console.log(myString.split('').reverse().join(''));
Output:
!dlroW olleH
The toLowerCase( ) method returns the calling string value converted to lowercase.
console.log('ALPHABET'.toLowerCase());
Output:
alphabet
The toUpperCase( ) method returns the calling string value converted to uppercase.
console.log('alphabet'.toUpperCase());
Output:
ALPHABET
The trim( ) method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
var orig = ' Hello World ';
console.log(orig.trim());
Output:
Hello World