JavaScript String Methods with example.

includes()

The includes() method determines whether a string contains the given characters within it or not. This method returns true if the string contains the characters, otherwise, it returns false.

const str = "JavaScript is fun";
// check if message includes the string "fun"
let result = str.includes("fun");
console.log(result);

// Output: true

Slice()

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end where start and end represent the index of items in that array.

const message = "JavaScript";

// slice the substring from index 0 to 4
let result = message.slice(0, 4);
console.log(result);

// Output: Java

concat()

The concat() method is used to merge two or more arrays. This method does not change the existing arrays but instead returns a new array.

let name = "";

// joint arguments string
let joinedString = name.concat("Riyaz", " Ahemad");
console.log(joinedString);

// Output: Riyaz Ahemad

Replace()

The replace() method replaces a specific value with another value in a string. Replace method replaces only the first match.

const message = "Foot ball";

// replace the first Foot with Bat
let result = message.replace('Foot', 'Bat');
console.log(result);

// Output: Bat ball

toLowerCase()

The toLowerCase() is a string method that converts a string to lowercase letters. The toLowerCase() method does not change the original string.

const name = "DOG";

// convert name into lowercase
const lowerName = name.toLowerCase();
console.log(lowerName);

// Output: dog

toUpperCase()

The toUpperCase() is a string method that converts a string to uppercase letters. The toUpperCase() method does not change the original string.

const name = "dog";

// convert message to uppercase
const upperName = name.toUpperCase();
console.log(upperName);

// Output: DOG

indexOf()

The indexOf() is a string method that returns the position of the first occurrence of a specified value in a string. This method returns -1 if the value is not found.

const name = "Hello world";

// returns index of 'w' 
const index = name.indexOf("w");

console.log('index: ' + index);  // index: 6

lastIndexOf()

The lastIndexOf() function will search and return the index of the last occurrence of a mentioned character within the string. If a mentioned character is not found, it will return -1.

// defining a string
var str = "Programming";

var substr = "g";

// find last occurrence of "g" in str
var result = str.lastIndexOf(substr);

console.log(result);   // Output: 10

It will search() and test for a match in a string and returns the index of the match. If a mentioned character is not found, it will return -1.

let message= "I love JavaScript.";

// pattern that searches the first occurence of an uppercase character
let regExp = /[J]/;

// searching for a match between regExp and given string 
let indexReg = message.search(regExp);

console.log(indexReg);   // Output: 0

substring()

substring() is the same as the slice() method. The only difference is that substring() does not accept negative indexes.

const message = "Namste Javascript";

// get the substring starting from index 0 to 6
let result = message.substring(0, 6);
console.log(result);

// Output: Namste

trim()

The trim() Method removes whitespace from both ends of a string.

const greeting = "      Hello world ";

// remove whitespaces from  both side
const NewGreeting = greeting.trim();
console.log(NewGreeting);

// Output: Hello world

trimEnd()

The trimEnd() Method removes whitespace from the end of a string.

trimStart()

The trimStart() Method removes whitespace from the Start of a string.

repeat()

The repeat() method returns a new string with a specified number of copies of the string it was called on.

let str = "NAMASTE "; 

//​repeating the given string 2 times
let result = str.repeat(2);
​
console.log(result)
//OUTPUT: NAMASTE NAMASTE