Methods
Arrays
array.concat(item...)
Produces new array copying the original array with the items
appended to it (does not modify the original array like array.push(item)
does. If the item
is an array, its elements are appended.
array.join(separator)
Creates a string of all the array's elements, separated by the separator
. Use an empty string separator
('') to join without separation.
array.pop()
Removes last element of array. Returns undefined
for empty arrays.
array.push(item...)
Modifies the array, appending items
onto the end. Returns the new length
of the array.
array.reverse()
Modifies the array by reversing the order of the elements.
array.shift()
Removes the first element of the array (does not leave a hole in the array - same effect as using the .splice(a,b)
method) and returns that first element.
array.slice(start, end)
Different to splice
.
'slice' creates a new array, copying from the start
element and stopping at the element before the end
value given. If no end
is given, default is array.length
.
Negative values for start
and end
will have array.length
added to them and if start
>end
, it will return an empty array.
array.sort(comparefn)
JavaScript has a sort()
method which was created only to compare strings and therefore sorts numbers incorrectly (it will sort them as 1, 15, 2, 23, 54 for example). Therefore, we have to write a comparison function which returns 0 if the two elements you are comparing are equal, a positive number if the first element should come first and a negative number if the second element should come first. Then pass this comparison function to sort()
as a parameter to allow it to sort array elements intelligently.
Page 80-82 in the book takes you through various iterations of the comparison functions - for numbers, simple strings, objects and objects with multiple keys (for example if you want to sort objects by first and last names). These should be taken from the book when required.
array.splice(start, deleteCount, item...)
Removes elements from the array making sure there are no holes left in the array. It is most popularly used for deleting elements from an array.
It removes the deleteCount
number of elements from the array starting from the start
position. If there are item
parameters passed to it, it will replace the deleted elements in the array with the items
.
It returns an array containing the deleted elements.
array.unshift(item...)
Works like push
but adds items to the front of the array instead of the end. Returns the new length
of the array.
Function
function.apply(thisArg, [argArray])
The apply
method invokes a function, passing in the object that will be bound to this
and optional array of arguments.
Number
number.toExponentional(fractionDigits)
Converts number to a string in exponential form (e.g. 3.14e+0). fractionDigits
(from 0 to 20) gives the number of decimal places.
number.toFixed(fractionDigits)
Converts number to a string in decimal form (e.g. 3.1415927). fractionDigits
(from 0 to 20) gives the number of decimal places.
number.toPrecision(precision)
Converts number to a string in decimal form (e.g. 3.1415927). The difference from toFixed
is that fractionDigits
(from 0 to 20) gives the number of *total digits.
number.toString(radix)
Converts number to a string. radix
is an optional parameter between 2 and 36 and gives the base. The default radix is 10.
Object
object.hasOwnProperty(name)
Does not look at the property chain*. Returns true if the object contains the property name
.
RegExp
regexp.exec(string)
Most powerful (and slowest) regexp method.
Checks the string
against the regexp (starting at position 0) and returns an array containing the matches.
The regexp is set up with various capturing groups and these determine the elements that go in the array:
- the 0 element of the array will contain the part of
string
that matched the _regexp - element 1 of the array will contain the text captured by the first capturing group in regexp
- element 2 of the array will contain the text captured by the second capturing group in regexp and so on
- if the match fails, it returns
null
If the regexp contains a g
flag (e.g. var regexp = /[ˆ<>]+|<(\/?)([A-Za-z]+)([ˆ<>]*)>/g;
), there is a lot more to look out for:
- Searching begins at
regexp.lastIndex
(initially zero) - If a match is found,
lastIndex
becomes the position of the first character of the match - If no match is found,
lastIndex
is reset to zero - If searching for multiple occurrences of a pattern by calling
exec
in a loop, ensure you resetlastIndex
when exiting the loop and rememberˆ
only matches whenlastIndex
is equal to zero
Example on page 87 of the book is worth reading to improve understanding.
regexp.test(string)
Simplest (and fastest) regexp method.
If regexp matches the string
it returns true. Otherwise it returns false.
Do not use the g
flag with this method.
String
string.charAt(pos)
Returns character at position po
in the string starting from 0. If pos
is less than zero or bigger than the string itself it return an empty string.
string.charCodeAt(pos)
Same as charAt
except it returns the integer that represents the code point value of the character at position pos
.
Returns NaN
is string.length < pos
< 0.
string.concat(string...)
Creates new string concatenating various strings. +
tends to be used instead of this method (e.g. var cat = 'c'+'a'+'t';
)
string.indexOf(searchString, position)
Searches for searchString
within string starting at position position
(an optional parameter). If position
is not provided, search starts at the beginning of the string.
Returns the integer position of the first matched character or -1 if no match is found.
string.lastIndexOf(searchString, position)
Same as indexOf
but searches from the end of the string instead of the beginning.
string.localeCompare(that)
Compares string to that
parameter and returns:
- 0 if string ===
that
- -1 if string <
that
NB. 'a' < 'A', comparison is not just in length.
string.match(regexp)
Works the same way as regexp.exec(string)
if there is no g
flag in the regexp
.
If there is a g
flag in teh regexp
, it produces an array of the matches but excludes the capturing groups
string.replace(searchValue, replaceValue)
Searches for the searchValue
in string and replaces it with the replaceValue
.
If searchValue
is a:
- string, only its first occurrence will be replaced with the
replaceValue
- regexp with a g flag, all occurrences will be replaced with the
replaceValue
; otherwise, only the first occurrence will be replaced
If replaceValue
is a:
- string, a
$
value has a special meaning when used in thereplaceValue
that conveys what to replace - see table on page 90 for possible variations on$
- function, it is called for each match and the string result of the function is used as the replacement text
- string result of the first call will replace capture group 1 of the string and so on
string.search(regexp)
Similar to .indexOf(string)
but takes a regexp
instead of a string
, returning the position of the first match (or -1 if there is no match).
The g
flag is ignored.
string.slice(start, end)
Creates a new string by copying the characters from the start
position to the character before the end
position in string.
The end
parameter is optiona and defaults to string.length. If either parameter is negative, string.length is added to it.
string.split(separator, limit)
Creates an array of strings by splitting apart string at the points where the separator
appears (e.g. if the separator is '.', ab.cd' becomes ['ab', 'cd']).
- If separator is an empty string, an array of single characters is produced.
limit
is optional and determines how many pieces are to be split off from the original string.- The
separator
can be aregexp
but- text from capturing groups within the regexp will be included in the split - e.g. in
var e = text.split(/\s*(,)\s*/);
the commas (,) will each be included as a separate element in the resulting array - some systems ignore empty strings when the
separator
is aregexp
- text from capturing groups within the regexp will be included in the split - e.g. in
string.substring(start, end)
No reason to use, use slice
instead.
string.toLocaleLowerCase()
Produces a new string converted to lower case, using the rules for the particular locale (geography).
string.toLocaleUpperCase()
Produces a new string converted to upper case, using the rules for the particular locale (geography).
string.toLowerCase()
Produces a new string converted to lower case.
string.toUpperCase()
Produces a new string converted to upper case.
String.fromCharCode(char...)
Produces a new string from a series of numbers.
var a = String.fromCharCode(67, 97, 116); //a === 'Cat'
NB. You're calling the prototype here, not replacing 'String' with your own variable.