JS includes(), slice(), concat(), trim(), split() string methods.

includes(): method returns true if a string contains a specified string. Otherwise, it returns false. The includes() method is case-sensitive. let text = "Hello world, welcome to the universe."; text.includes('world') // true slice(): extracts a part of a string. return the extracted part in a new string. doesn’t change the original string. The start and end parameters specify the part of the string to be extracted. position index starts with 0. A negative number is selected from the end. let txt = 'Hello world'; txt.slice(0, 5) // 'Hello' txt.slice(7,10) // 'orl' txt.slice(-1) // 'd' concat(): method is used to combine two or more strings into one single new string. It does not modify the original strings. It returns a new string containing the text of the joined strings. const str1 = 'Hello'; const str2 = 'World'; console.log(str1.concat(' ', str2)) // Hello World trim(): method removes whitespace from both ends of a string. const txt = ' hello world '; txt.trim() // 'hello world' split(): method divides a string into an array of substrings based on a specified separator (like space, comma, etc.) Returns a new Array. Doesn’t change the original array. const txt = 'Hello World'; txt.split('') // ['H', 'e', 'l', 'l', ' ', 'W', 'o', 'l', 'd'] txt.split(' ') // ['Hello', 'World'] txt.split('o') // ['Hell', ' W', 'rld'] const txt2 = 'apple, banana, orange'; txt2.split(',', 2); // ['apple', 'banana']

Apr 11, 2025 - 22:39
 0
JS includes(), slice(), concat(), trim(), split() string methods.

includes():

method returns true if a string contains a specified string. Otherwise, it returns false. The includes() method is case-sensitive.

let text = "Hello world, welcome to the universe.";
text.includes('world') // true

slice():

extracts a part of a string. return the extracted part in a new string. doesn’t change the original string. The start and end parameters specify the part of the string to be extracted. position index starts with 0. A negative number is selected from the end.

let txt = 'Hello world';
txt.slice(0, 5) // 'Hello'
txt.slice(7,10) // 'orl'
txt.slice(-1) // 'd'

concat():

method is used to combine two or more strings into one single new string. It does not modify the original strings. It returns a new string containing the text of the joined strings.

const str1 = 'Hello';
const str2 = 'World';

console.log(str1.concat(' ', str2)) // Hello World

trim():

method removes whitespace from both ends of a string.

const txt = '    hello world    ';
txt.trim() // 'hello world'

split():

method divides a string into an array of substrings based on a specified separator (like space, comma, etc.)

  • Returns a new Array.
  • Doesn’t change the original array.
const txt = 'Hello World';
txt.split('') // ['H', 'e', 'l', 'l', ' ', 'W', 'o', 'l', 'd']
txt.split(' ') // ['Hello', 'World']
txt.split('o') // ['Hell', ' W', 'rld']
const txt2 = 'apple, banana, orange';
txt2.split(',', 2); // ['apple', 'banana']