DEV Community

Kawsar Hossain
Kawsar Hossain

Posted on

Some topics on Javascript methods.

Overview: Today in this article we will know about some topics/methods of Javascript.

1- Javascript string
charAt: The charAt() method returns the character at the index which will be given in this(index) method in a string.
const text = 'hello world';
console.log(text.charAt(6))
// and the answer will be "w" becasue in this text the sixth charecter is w and the charat method check the text and give the answer depened on the index that we have provided in this method

concat(): This method is used for concatenate strings together.
const textOne = 'Hello';
const textTwo = 'World';
console.log(textOne.concat(textTwo))
// and the answer will be "helloWorld" we can concate more string like this

*includes(): * This method returns true if a string contains a specified string. Otherwise, it will returns false
const text = "hello world";
console.log(text.includes('world'))
// and the answer will be true because the method checked the text variable with the string that we are given in the method and the method finds it true and return true

endsWith(): This method will return true if the a string ends with a specified string otherwise it will return false, and this method is case sensitive.
const line = "hello there what's upp";
console.log(line.endsWith('upp'))
// and the answer will return true because here in this method the the text ends with the string 'upp' and we provide the 'upp' string text and its matched that's why it return true

indexOf(): This method will return the position of the first occurrence of a specified value in a string.If the value isn’t available in the string then it will return the -1 value otherwise it will return the index.We can also set the starting index in indexOf method so that it can start the method from there.
const array = ["one", "two", "three", "two"];
console.log(text.indexOf('two'));
// and the answer will be 1 index number because here in this method we provided the 'two' and this method finds the first 'two' in the one number index and it will return 1
const array = ["one", "two", "three", "two"];
console.log(array.indexOf('two', 2));
// Now here it will search the index of ‘two’ from the second index. and it will return 3
lastIndexOf(): This method also works like indexOf but this method start searching from the last index
const array = ["one", "two", "three", "two"];
console.log(array.indexOf('two'));
// Now here it will search the index of ‘two’ from the last index. and it will return 0 because this method start searching from the last index

replace(): this method is used to replace a string with another string we can add/give two-parameter one is what we want to change another is what will be the new string. And we can also replace with all match strings using a regular expression like this
const textOne = 'hello world';
console.log(textOne.replace('hello', 'hi'));
// and the output will be come 'hi world'

const textTwo = 'hello there ! hello'
console.log(textTwo.replace(/hello/g, 'hi'))
// and now the output will be come like this: 'hi world ! hi' because it replace the all matching string that we given in the first second paremeter

trim(): using this method we can remove the extra spaces in our strings we don’t need any parameter in this method we can use this normally like this
const text = ' hello '
console.log(text.length)
// here is the length of this text varibale is 15 now we will going to trim it using trim
console.log(text.trim())
// now the length will be 5 because we trimed the text variable

isNaN(): The isNaN method is used to check the given value is a number or not if the value is a number then it will return false and if the number is not a number then it will return true.
const number = 100;
console.log(isNaN(number))
// here the answer will be false because the given value is an number.

Split(): The split method splits or divides the string depending on the value of the method that we are going to add. And it will return an array. Just like if we want to divide a string into the spaces of string we can run the code like this
const text = 'hello World! How are you?'
console.log(text.split(' '))
// here the answer will be look like this [ 'hello', 'World!', 'How', 'are', 'you?' ] because we divide the string by the spaces

findIndex(): This method returns the index of the first element in the array that is matched with the provided testing function in findIndex. And if the value is not matched with the array then it will return -1 we can check/ use like this
const numbers = [10, 20, 30, 40, 50];
const result = numbers.findIndex(number => number > 20 && number < 40)
console.log(result)
// and here the answer will be 2 because the match first match index is 2

join(): This method creates or concatenates and returns a new string by concatenating all of the elements in an array.
const books = ["Bangla", "English", "Math", "Physics", "Chemistry"]
console.log(books.join())
// and here the answer will be like this "Bangla,English,Math,Physics,Chemistry" because this method concatenates the values and return as a string.

reduce(): using this method we can use another call back function on it we can give it two-parameter and we can make them how to work and we have to give the initial value. After run this method it will return a single value
const numbers = [10, 20, 30, 40, 50];
const sum = numbers.reduce((previousValue, currentValue) => {
return previousValue + currentValue
}, 0)
console.log("the sum is", sum)
// now the result will be 150 because it takes all the array elements into the reducer and do the addition with the previousValue and Next value and its starts with the 0

Top comments (0)