Creates an array by calling a specific function on each element present in the parent array.
Parameter | Description |
---|---|
function(currentValue) | Required function to be run for each array element |
An Array containing the results of calling the provided function for each element in the original array.
Creates an array filled with all array elements that pass a test (provided as a function).
Parameter | Description |
---|---|
function(currentValue) | Required function to be run for each array element |
Array containing all elements that pass the test else an empty array.
Output:
[112,52,944]
reduces the array to a single value and executes provided function for each value of the array (from left-to-right).
Parameter | Description |
---|---|
function(currentValue) | Required function to be run for each array element |
returns accumulated result fron the last call of the callback function.
const array1 = [1, 2, 3, 4];
const reducer = function(accumulator, currentValue)
{
return accumulator + currentValue;
}
console.log(array1.reduce(reducer));
console.log(array1.reduce(reducer, 5));
Output:
10
15
Returns the selected elements in an array, as a new array object and selects the elements starting at the given start argument, and ends at the given end argument(excluding end argument).
Parameter | Description |
---|---|
start | Optional. specifies starting index of the selection |
end | Optional. specifies ending index of the selection |
#### Return value New array containing the selected elements.
#### Example
Output:
[1,2,3,4,5,6]
[4,5,6]
Adds/removes items to/from an array, and returns the removed item(s).
Parameter | Description |
---|---|
index | Required. An integer that specifies at what position to add/remove items |
howmany | Optional. The number of items to be removed. |
item1, …, itemX | Optional. The new item(s) to be added to the array |
Array without the removed items.
const languages = ['C++', 'Java', 'Html', 'Python', 'C'];
// Add 'Julia' and 'Php' after removing 'Html'.
const removed = languages.splice(2, 1, 'Julia', 'Php')
Output:
C++,Java,Html,Python,C
C++,Java,Julia,Php,Python,C
Note | splice | slice | | :— | :— | | Returns the removed item(s) in an array| Returns the selected element(s) in an array, as a new array object| | Changes the original array| Doesn’t change the original array| | Can take n number of arguments(1 required)| Can take 2 arguments(1 required)|
Used to add a single item to an array(or to add two or more arrays).
or javascript array1.concat(array2, array3, ..., arrayX)
Parameter | Description |
---|---|
array2, array3, …, arrayX | Required. The arrays to be joined. |
Joined array
Output:
[1,2,3,4,5,6,7]
Note | concat | .push | | :— | :— | | Adds elements to the end of an array| Adds element or merges arrays| | Returns the new length of the array| Returns a new array|
Returns the array as a string. The elementsare separated by a specified separator. The default separator is comma (,).
Parameter | Description |
---|---|
separator | Optional. |
String, representing the array values, separated by the specified separator.
const languages = ['C++', 'Java', 'Html', 'Python', 'C'];
languages.join();
languages.join('.');
languages.join('-');
Output:
C++,Java,Html,Python,C
C++.Java.Html.Python.C
C++-Java-Html-Python-C
Adds new items to the end of an array, and returns the new length.
Parameter | Description |
---|---|
item1, item2, …, itemX | Required. The item(s) to add to the array |
New length of the array.
Output:
8
[1,2,3,4,5,6,7,8]
Returns the value of the first element in an array that pass a test (provided as a function).
Parameter | Description |
---|---|
function(currentValue) | Required. A function to be run for each element in the array. |
The array element value if any of the elements in the array pass the test, otherwise it returns ‘undefined’.
let array = [1,3,5,7,9];
const found = array.find(function(element){
return element > 4;
})
// Printing desired value.
Output:
5
returns the position of the first occurrence of a specified value in a string.
Parameter | Description |
---|---|
searchvalue | Required. The string to search for |
start | Optional. Default 0. At which position to start the search. |
Number, representing the position where the specified searchvalue occurs for the first time, or -1 if it never occurs.
Output:
9
Extracts the characters in a string between “start” and “end”, not including “end” itself.
Parameter | Description |
---|---|
start | Required. The position where to start the extraction. First character is at index 0 |
end | Optional. The position (up to, but not including) where to end the extraction. If omitted, it extracts the rest of the string |
New string containing the extracted characters.
// Taking a string as variable
const string = "JavaScriptCheatsheet";
a = string.substring(0, 4)
b = string.substring(1, 6)
c = string.substring(5)
d = string.substring(0)
// Printing new string which are
// the part of the given string
Output:
Java
avaSc
criptCheatsheet
JavaScriptCheatsheet
Note | array.slice() | string.substring() | | :— | :— | | Displays selected array elements | Displays selected part of string| | Returns new array| Returns new string|
Used to split a string into an array of substrings, and returns the new array.
Parameter | Description |
---|---|
separator | Optional. Specifies the character, or the regular expression, to use for splitting the string. If omitted, the entire string will be returned (an array with only one item) |
limit | Optional. An integer that specifies the number of splits, items after the split limit will not be included in the array |
Array containing splitted values.
Output:
["It","iS","a","great","Day."]
Converts a string to lowercase letters.
string.toLowerCase()
A string, representing the value of a string converted to lowercase.
Output:
it is a great day.
Removes whitespace from both sides of a string.
String, representing the string with removed whitespace from both ends
Output:
"Javascript"
Returns the character at the specified index in a string.
String, representing the character at the specified index, or an empty string if the index number is not found
Output:
t
Output:
"a"
"b"
"c"
Loops through the values of an object.
const languages = { first : "C", second : "Java",
third : "Python", fourth : "PHP",
fifth : "JavaScript" };
// iterate through every property of the
// object languages and print all of them
// using for..in loops
for (itr in languages)
{
console.log(languages[itr]);
}
Output:
C
Java
Python
PHP
JavaScript
Traditional index-based loop as found in many languages.
This will print Hello World on the screen 10 times.
Calls a function once for each element in an array, in order.
Parameter | Description |
---|---|
function(currentValue) | Required. A function to be run for each element in the array. |
Output : It squares each number in the array items .
1,841,2209