0% found this document useful (0 votes)
11 views

JS Array & Methods

The document discusses various string and array methods in JavaScript. It provides details on methods like concat(), includes(), indexOf(), slice(), map(), filter(), and more. These methods can be used to manipulate, search, extract, transform and check strings and arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

JS Array & Methods

The document discusses various string and array methods in JavaScript. It provides details on methods like concat(), includes(), indexOf(), slice(), map(), filter(), and more. These methods can be used to manipulate, search, extract, transform and check strings and arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

JS STRING METHODS

JavaScript String concat ()


concat() joins two or more strings:
There are 3 methods for extracting string characters:

 charAt(position)
 charCodeAt(position)
 Property access [ ]

JavaScript String charAt ()


The charAt() method returns the character at a specified index
(position) in a string:

JavaScript String charCodeAt ()


The charCodeAt() method returns the unicode of the character at a
specified index in a string:

The method returns a UTF-16 code (an integer between 0 and 65535).
Property Access [] on strings

Property access:

 It makes strings look like arrays (but they are not)


 If no character is found, [ ] returns undefined, while charAt()
returns an empty string.
 It is read only. str[0] = "A" gives no error (but does not work!)

If you want to work with a string as an array, you can convert it


to an array.

JavaScript String endsWith ()


The endsWith() method returns true if a string ends with a specified
string.

Otherwise it returns false.

The endsWith() method is case sensitive.

See also the startswith() method.

JavaScript String includes ()


The includes() method returns true if a string contains a specified string.

Otherwise it returns false.


The includes() method is case sensitive.

JavaScript String indexOf ()


The indexOf() method returns the position of the first occurrence of a
value in a string.

The indexOf() method returns -1 if the value is not found.

The indexOf() method is case sensitive.

JavaScript String lastIndexOf ()


The lastIndexOf() method returns the index (position) of the last
occurrence of a specified value in a string.

The lastIndexOf() method searches the string from the end to the
beginning.
The lastIndexOf() method returns the index from the beginning (position
0).

The lastIndexOf() method returns -1 if the value is not found.

The lastIndexOf() method is case sensitive.

JavaScript String match ()


The match() method matches a string against a regular expression **

The match() method returns an array with the matches.

The match() method returns null if no match is found.


Replacing String Content
The replace() method replaces a specified value with another value in a
string:

JavaScript String split ()


A string can be converted to an array with the split() method:

If the separator is omitted, the returned array will contain the whole
string in index [0].
If the separator is "", the returned array will be an array of single
characters:

There are 3 methods for extracting a part of a string:

 slice(start, end)
 substring(start, end)
 substr(start, length)

 slice() extracts a part of a string and returns the extracted part


in a new string.
 The method takes 2 parameters: the start position, and the end
position (end not included).

If you omit the second parameter, the method will slice out the rest of
the string.

substring() is similar to slice().

The difference is that start and end values less than 0 are treated as 0 in
substring().
If you omit the second parameter, substring() will slice out the rest of
the string.

substr() is similar to slice().

The difference is that the second parameter specifies the length of the
extracted part.

If you omit the second parameter, substr () will slice out the rest of the
string.

JavaScript String trim ()


The trim() method removes whitespace from both sides of a string:
Converting to Upper and Lower Case
A string is converted to upper case with toUpperCase()

A string is converted to lower case with toLowerCase()

JavaScript String valueOf ()


The valueOf() method returns the primitive value of a string.

The valueOf() method does not change the original string.

The valueOf() method can be used to convert a string object into a string.
JS ARRAY METHODS
JavaScript Array concat ()
The concat() method concatenates (joins) two or more arrays.

The concat() method returns a new array, containing the joined arrays.

The concat() method does not change the existing arrays.

JavaScript Array copyWithin ()


The copyWithin() method copies array elements to another position in the
array.

The copyWithin() method overwrites the existing values.

The copyWithin() method does not add items to the array.


JavaScript Array every ()
The every() method executes a function for each array element.

The every() method returns true if the function returns true for all
elements.

The every() method returns false if the function returns false for one
element.

The every() method does not execute the function for empty elements.

The every() method does not change the original array

JavaScript Array fill ()


The fill() method fills specified elements in an array with a value.

The fill() method overwrites the original array.

Start and end position can be specified. If not, all elements will be filled.
JavaScript Array filter ()
The filter() method creates a new array filled with elements that pass a
test provided by a function.

The filter() method does not execute the function for empty elements.

The filter() method does not change the original array.

JavaScript Array find ()


The find() method returns the value of the first element that passes a
test.

The find() method executes a function for each array element.

The find() method returns undefined if no elements are found.

The find() method does not execute the function for empty elements.

The find() method does not change the original array.


JavaScript Array findIndex ()
The findIndex() method executes a function for each array element.

The findIndex() method returns the index (position) of the first element
that passes a test.

The findIndex() method returns -1 if no match is found.

The findIndex() method does not execute the function for empty array
elements.

The findIndex() method does not change the original array.

JavaScript Array forEach ()


The forEach() method calls a function for each element in an array.

The forEach() method is not executed for empty elements.

JavaScript Array indexOf ()


The indexOf() method returns the first index (position) of a specified
value.

The indexOf() method returns -1 if the value is not found.


The indexOf() method starts at a specified index and searches from left
to right.

By default the search starts at the first element and ends at the last.

Negative start values counts from the last element (but still searches
from left to right).

JavaScript Array.isArray ()
The isArray() method returns true if an object is an array,
otherwise false.

Array.isArray() is a static property of the JavaScript Array object.

You can only use it as Array.isArray().


JavaScript Array join ()
The join() method returns an array as a string.

The join() method does not change the original array.

Any separator can be specified. The default is comma (,).

JavaScript Array keys ()


The keys() method returns an Array Iterator object with the keys of an
array.

The keys() method does not change the original array.


JavaScript Array map ()
map() creates a new array from calling a function for every array
element.

map() calls a function once for each element in an array.

map() does not execute the function for empty elements.

map() does not change the original array.

JavaScript Array pop ()


The pop() method removes (pops) the last element of an array.

The pop() method changes the original array.

The pop() method returns the removed element.


JavaScript Array push ()
he push() method adds new items to the end of an array.

The push() method changes the length of the array.

The push() method returns the new length.

JavaScript Array reverse ()


The reverse() method reverses the order of the elements in an array.

The reverse() method overwrites the original array.


JavaScript Array shift ()
The shift() method removes the first item of an array.

The shift() method changes the original array.

The shift() method returns the shifted element.

JavaScript Array slice ()


The slice() method returns selected elements in an array, as a new
array.

The slice() method selects from a given start, up to a (not inclusive)


given end.

The slice() method does not change the original array.


JavaScript Array some ()
The some() method checks if any array elements pass a test (provided as
a callback function).

The some() method executes the callback function once for each array
element.

The some() method returns true (and stops) if the function returns true for
one of the array elements.

The some() method returns false if the function returns false for all of the
array elements.

The some() method does not execute the function for empty array
elements.

The some() method does not change the original array.

JavaScript Array sort ()


The sort() sorts the elements of an array.

The sort() overwrites the original array.


The sort() sorts the elements as strings in alphabetical and ascending
order.

For numbers:

JavaScript Array splice ()


The splice() method adds and/or removes array elements.

The splice() method overwrites the original array.


JavaScript Array toString ()
The toString() method returns a string with array values separated by
commas.

The toString() method does not change the original array.

JavaScript Array unshift ()


The unshift() method adds new elements to the beginning of an array.

The unshift() method overwrites the original array.

You might also like