String‌ ‌Methods‌ ‌and‌ ‌Properties‌ ‌-‌ ‌
JavaScript‌ 
Published: 2021-12-29 09:50:44

Today we will discuss the JavaScript string method. We all know what a JavaScript string is. If we bind the value of a single variable with a double or single quotation then it is known as a string in JS. 

let myName = ‘sujan majumder’

Here the value of myName variable ‘Rakibul Islam’ is a string type value. We can also check what type of value we want.

console.log(typeof myName); // string

This way we can see the type of the variable. But today we will learn what methods can be applied on a string. 

chatAt method: 
In JavaScript, chartAt() method, we pass a number type value in the function, then it returns the specific character, by the index position. Remember that the index number starts counting from 0. 

example:   
let text = "Rakibul Islam";
let letter = text.charAt(0); 
console.log(letter); 
// Output: R 

Concat Method: 
In the javaScript method, we use concat() method to merge two or more than a string type value. 

example: 
const str1 = 'Hello';
const str2 = 'World';
console.log(str1.concat(' ', str2));
// expected output: "Hello World"


Includes Method: 
In javaScript, includes() method returns a boolean type value, it is true or false, when we use the includes method on a string, it determines whether characters included in the string.  

example: 
const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.includes('fox'));
// expected output: true 
endWith Method: 
In javaScript, we can apply endsWith method on a string. When it is determined, the string ends with the characters by returning true or false as appropriate. Ans, also we can pass a number as a second parameter, which is checking the end position of the string. And here the second parameter will count the index number from 1. 

example: 
const str1 = 'Cats are cute!';
console.log(str1.endsWith(cute!', 14));
// expected output: true
console.log(str1.endsWith(cute', 14)); [ note : here we remove the ‘ ! ‘ character ] 
// expected output: false 

indexOf() or lastIndexOf() method: 
The indexOf () method returns the index number of a specific character. If the character is not found, then it returns a negative one, which means - 1.   

Example: 
const paragraph = 'Rakibul Islam';
console.log(paragraph.indexOf("Islam"))
// expected output: 8 

replace() method: 

The replace() method returns a new string, when we pass to the parameter in this function, the first parameter is the specific character, and the second parameter is the new character which will be replaced by the first character and its index position. 

Example: 
const name = 'Rakibul Islam';
console.log(name .replace('Rakibul', 'Shofikul'));
// expected output: 'Shofikul Islam' 

slice() method: 
In javascript by applying slice() method on a string, it extracts a section of the string and finally returns a new string. And does not modify the original string. 

Example: 
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(31));
// expected output: "the lazy dog."
console.log(str.slice(4, 19));
// expected output: "quick brown fox"

Split() method: 
The split method splits a string into an array of substrings without affecting the original string. 

Example:
let text = "How are you doing today?";
const myArray = text.split(" ");
// expected output: How,are,you,doing,today?

subtract() method: 
subtract method returns the portion of the string, from the first parameter as the first index position of the string and the second parameter of the last index of the string. 

Example: 
const str = “rakib”;
console.log(str.substr(1, 2));
// expected output: "ak" 

toLowercase(), toUppercase() method: 
Here the toLowerCase() method converts the string to lower case. Also, The toUppercase() method converts the string to uppercase. 
Example: 
const myName = “RakiBuL ISlam”
console.log(myName.toLowerCase()); // rakibul islam
console.log(myName.toUppercase()); // RAKIBUL ISLAM 

trim() method: 
Here the trim() method removes the blank space from both sides of the string. And return a new string and doesn't affect the original string. 

Example: 
const greeting = '   Hello world!   ';
console.log(greeting.trim()); // expected output: "Hello world!";