JavaScript some amazing string & Array
1.String INCLUDES METHOD:
String includes method determines whether one string may be found within another string That returns true or false as appropriate.
const sentence=’i am learning javascript’;
console.log(sentence.includes(‘learning’));
expected output: true
2.Sting indexOf-
It defines the position of string values and numbers .You can get position any value but it should be case sensitive.
const sentence=’i am learning javascript’
console.log(sentence.indexOf(‘learning’));
expected output:5
3.LastIndex of Method:
It generally starts with last index of an array .it is generally used when vales becomes double :
const array=[‘one’, ‘two’, ‘three’, ‘one’]
console.log(array.lastIndexOf(‘one’));
expected output:3
4. concat METHOD
The concat() method is used to join two or more strings. It does not make changed the exiting strings and returns new string
var str1 = “Hello “;
var str2 = “Rashed!”;
var result = str1.concat(str2);
console.log(result);
expected output: Hello Rashed!
5. toLowercase method:
The toLowerCase() method returns the string value that converted to lower case.You can convert to any capital letter to small letter.
// const str = ‘rasehd chy’;
// console.log(str.toUpperCase());
expected output: RASHED CHY
6. Array Map Method:
Map is a collection of keyed data items, just like an Object.It holds key-value pairs and remembers the original insertion order of the keys
const numbrs=[2,4,3,5,6]
const result=numbrs.map(number=>number*2)
console.log(result);
expected output: [ 4, 8, 6, 10, 12 ]
7.Javascript Find Array Method :
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 numbrs=[8,4,5,9,6]
const result=numbrs.find(number>6)
console.log(result);
expected output: [ 4, 8, 6, 10, 12 ]
8. Javascript Array Filter method:
The filter() Array method creates a new array with elements that fall under a given criteria from an existing array:
var numbers = [1, 3, 6, 8, 11];
var lucky = numbers.filter(function(number) {
return number > 7;
});
console.log(lucky);
expected output: [8, 11]
Array Reduce Method:
It makes the array to a single value. reduce() does not execute the function for array elements without values.in this method, original
Array remains the same.
var numbers = [1, 3, 6, 8, 11];
const sum=numbers.reduce((preValue,currentValue)=>{
return preValue+currentValue;
},0)
console.log(sum);
expected output: 29;
9.Array Reverse Method: it of the elements in an array. Thats means, the first array element becomes the last, and the last array element becomes the first.
const names=[‘rashed’,’minto’,’wahid’,’jamal’]
const result=names.reverse()
console.log(result);
expected output: [ ‘jamal’, ‘wahid’, ‘minto’, ‘rashed’ ]
10.Javascript For each method :
The forEach() method calls a function once for each element in an array, in order. The function is not executed for array elements without values.
const numbers=[4,5,3,9,8,4,7]
const square=[]
numbers.forEach(function(x){
square.push(x*x)
})
console.log(square);
expected output: [
16, 25, 9, 81,
64, 16, 49
]