TOP 8 Javascript String Methods You Need To Know

1. startsWith()

check if string starts with specified character(s).

// startswith () checks if the string
// starts with specified character (s)
const str = "JavaScript is amazing';
console. log (str. startswith (JavaScript ')); // true
console. log (str. startsWith ( 'javascript ')); // fals

2. endsWith()

The ends with string method in java script is used to determines whether the string ends with a specified sub string or not.

If it ends with a specified string then it returns true, else returns false.

The sub string to be checked for the ending is passed as the first argument in the method.

/ / endsWith () checks if the string
/ /  ends with specified character (S)
const str = 'JavaScript is amazing';
/ / check if ends with 'amazing'
console. log (str. endsWith ( 'amazing ')); // true
console. log(str. endswith ( 'Amazing ' )); // false

3. includes()

The includes() method is used to determine whether a string that characters of a specified string or not.

If is exist return true or not return false.

/ /  includes () checks if the string
/ / contains specified character (s)
const str = 'JavaScript is amazing';
console. log(str. includes ('Script ')); // true
console. log (str. includes ( 'script ')); // false
console. log (str. includes (' ')); // true
console . log(str. includes ( 'array ' )); / / false

4. slice()

The slice() method removes the parts of a string and returns the extracted parts to a new string.

Use the Start and End Ultimate to specify the part of the string that you want to remove.

/ /  slice () copies some part of string
/ /without modifying the original one
const str = 'JavaScript is amazing';
/ / Default start index is 0
console.log (str. slice ()) ; // 'Javascript is amazing'
/ / start copy at index 4
console. log (str. slice (4)) ; / / 'Script is amazing'
/ / end copy at index 10(character at this index
/ /  will not be included)
console. log (str. slice (0, 10)); / / 'Javascript'

5. toUpperCase()

Convert string into upper case.

/ /  toUpperCase() converts the string
/ / into uppercase
const str = 'Javascript is amazing ';
console. log (str. toupperCase () ); // 'JAVASCRIPT IS AMAZING'

6. toLowerCase()

Convert string into lower case.

/ / to Lowercase () converts the string
/ / into lowercase
const str = 'JavaScript is amazing';
console. log (str. toLowerCase () ); // 'javascript is amazing'

7. char At()

The Javascript charAt() is used to take a character to a descrived index location

 / / charAt () returns character at
 / / a specified position
const str = 'JavaScript is amazing';
console. log (str. charAt ()); / / 'J'
console. log (str.charAt (11) ); / / 'i"
console. log (str.charAt (14) ); / / 'a'
console. log (str. charAt (110) ) ; / / ' '

8. split()

A String can ve converted to an array with the split() method.

/ / split () splits strings into an array of substrings
 Const str = 'JavaScript is amazing';
const strNew ='JavaScript-is-amazing';
console.log (str. split ()); //["JavaScript is amazing"]
/ /  Separator string used to determine where to make each split
console. log (str. split( 'S')); // ["Java", "script is amazing"]
console. log(str. split (' is ')); // ["JavaScript ", " amazing"]
console. log(str. split(' ')); / / ["JavaScript", "is", "amazing"]
console. log (strNew. split('- ')); // ["JavaScript", "is", "amazing"]

Leave a Reply

Your email address will not be published. Required fields are marked *