click below
click below
Normal Size Small Size show me how
JavaScript Methods
method syntax and what they return
Term | Definition |
---|---|
.filter() | array.filter(callback) { do whatever }) The filter function iterates over each element of an array and returns just the ones that result true. |
.test() | tests a string for a match and returns true or false. Takes one argument: either a string or RegEx. RegExHere.test(string); |
.map() | array.map(function(current, index, array) { code here can include this }); Map() provides a callback function once for each element in an array, in order. It returns a new array from results. |
.push() | Adds new elements to the end of an array and returns the new LENGTH. Therefore, its ideal to declare the array to be pushed to, globally. syntax: arr.push(any_variable_name) |
.pop() | Pops the last value of an array and returns that array. Accepts no arg. syntax: arr.pop(); |
shift() | Shift removes the first element of an array. Shift returns the item that was shifted out. Accepts no arg. syntax: arr.shift(); |
.splice() | Splice changes the array it operates on and returns that array. syntax: array.splice(index, toRemove, "what to add here"); Ex: var fruits = ["apple", "kiwi", "orange", "pear"]; return fruits.splice(0, 1, "opossum"); |
join() | array.join(); Makes an array a string by joining all the pieces and inserting the separator. Separator is an optional argument. Join() returns the string it creates. |
concat() | arr1.concat(arr2, arr3Optional); Joins two or more arrays. Returns a copy of the joined arrays. |
.map() | array.map(function(current, index, array) { code here can include this }); Map() provides a callback function once for each element in an array, in order. It returns a new array from results. |
unshift() | Unshift adds a new element to an array at the beginning and unshifts older elements. It returns the new array length. |
the javascript operator, delete | This is a operator that is used to delete items from an array by changing their value to undefined, which is falsey. Best to use pop() or shift() syntax: delete array[index] |
.reverse() | Reverses the items in an array and returns the same array, reversed. arr.reverse(); |
.sort() | Sorts an array of string by default, alphabetically. Otherwise sort() takes a compare function such as: array.sort(function(a,b){return a-b)}; Use to find highest or lowest value. |
.slice() | Slice slices a piece of an array and returns it in a new array. syntax: var arrayOfSlices = originalArr.slice(0, 2); This method ends at but does not include the second index argument. |
typeof | typeof is a Javascript operator that returns the object type. syntax: typeof example_item; |
.reduce() | syntax: arr.reduce(callback(prev, current, index, array) {...this..}optInitialvalue); Reduce is a iterator function that applies a function against an accumulator and each value of the array, to reduce it to a single value. |