JavaScript Array and String special Function
IndexOf method returns the index within in the calling string. If it is not found its return -1.
const paragraph = ‘The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?’;
const searchTerm = ‘dog’;
const indexOfFirst = paragraph.indexOf(searchTerm);
console.log(`The index of the first “${searchTerm}” from the beginning is ${indexOfFirst}`);
// expected output: “The index of the first “dog” from the beginning is 40"
console.log(`The index of the 2nd “${searchTerm}” is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
// expected output: “The index of the 2nd “dog” is 52"
For example, 'hello world'.indexOf('o', -5)
will return 4
, as it starts at position 0
, and o
is found at position 4
. On the other hand, 'hello world'.indexOf('o', 11)
(and with any fromIndex
value greater than 11
) will return -1
, as the search is started at position 11
, a position after the end of the string.
The Slice() method extracts part of a string and returns a new string
Syntax
slice(beginIndex,endIndex)
beginIndex: The position where to begin the extraction. The first character is at position 0
endIndex: The zero-based index before which to end extraction. The character at this index will not be included.
const str=” Emon”;
console.log(str.slice(0,2)
// ExpectedOutput Emo
The includes()
the method determines whether a string contains the characters of a specified string.
This method returns true
if the string contains the characters, and false
if not.
The includes() method is case sensitive. For example
“Blue Whale” .includes(‘blue’) // returns false
Syntax
includes(searchString)
includes(searchString, position)
if the search string is found it returns true or False.
The ends With() method determine a specific string whether a string end.
This method returns true
if the string ends with the characters, and false
if not.
endsWith(searchString)
endsWith(searchString, length)
This method lets you determine whether or not a string ends with another string. This method is case-sensitive.
The replace() method searches a string for a specified value and returns a new string where the specified value is replaced.
replace(substr, newSubstr)
The pattern
can be a string or a RegExp
, and the replacement
can be a string or a function to be called for each match. If pattern
is a string, only the first occurrence will be replaced.
The parseFloat() function parses a string and returns a floating-point number.
This function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string.
parseFloat
is a function property of the global object.
The value to parse. If this argument is not a string, then it is converted to one using the ToString
abstract operation. Leading whitespace in this argument is ignored
- If the argument’s first character can’t be converted to a number (it’s not any of the above characters),
parseFloat
returnsNaN
. parseFloat
can also parse and returnInfinity
.parseFloat
convertsBigInt
syntax toNumbers
, losing precision. This happens because the trailingn
character is discarded
The filter()
method creates a new array with all elements that pass the test implemented by the provided function.
const words = [‘Emon’, ‘Ruman’, ImnulHaque’];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array [‘ImnulHaque”]
The find()
method returns the value of the first element in an array that pass a test (provided as a function).
The find() method executes the function once for each element present in the array:
- If it finds an array element where the function returns a true value, find() returns the value of that array element (and does not check the remaining values)
- Otherwise it returns undefined
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element <10);
console.log(found);
// expected output: 5
The forEach()
method executes a provided function once for each array element
forEach()
calls a provided callback
function once for each element in an array in ascending index order. It is not invoked for index properties that have been deleted or are uninitialized. (For sparse arrays, see example below.)
callback
is invoked with three arguments:
- the value of the element
- the index of the element
- the Array object being traversed
const array1 = [‘a’, ‘b’, ‘c’];
array1.forEach(element => console.log(element));
// expected output: “a”
// expected output: “b”
// expected output: “c”
The map()
method creates a new array with the results of calling a function for every array element.
The map()
method calls the provided function once for each element in an array, in order.
A new array with each element being the result of the callback function.
map
calls a provided callback
function once for each element in an array, in order, and constructs a new array from the results. callback
is invoked only for indexes of the array which have assigned values (including undefined
).
It is not called for missing elements of the array; that is:
- indexes that have never been set;
- indexes that have been deleted.
const array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map(x => x * 2);
console.log(map1);
// expected output: Array [2, 8, 18, 32]