JavaScript String and String Methods with Example


In this JavaScript tutorial, we are going to learn "String in JavaScript", "JavaScript Strings Example" and Different JavaScript String Methods Examples:

JavaScript String and String Methods with Example

What are Strings in JavaScript?

  • A JavaScript string is a collection of one or more than one characters.

  • A string can contain letters, numbers, and symbols.

  • JavaScript strings are enclosed in single quotes (') or double quotes(").

  • Strings represent textual data and are one of the primitive data types.

  • Let's see JavaScript strings with an Example


JavaScript String Example:

let str1 = 'Geeks Help';
let str2 = "JavaScript is amazing!";

console.log(str1);
console.log(str2);

// Output:
Geeks Help
JavaScript is amazing!

In the given code, three variables are declared: str1, str2, and str3. Each variable holds a different string value. Here's a breakdown of each line:

1. In the first line, the variable str1 is assigned the string value 'Geeks Help'. And the string is enclosed in single quotes.

2. In the second line, the variable str2 is assigned the string value "JavaScript is amazing!". This time, the string is enclosed in double quotes.

"Finally, the console.log() function is used to print the values of each variable to the console."

Creating JavaScript String

We can create javascript strings in 2 ways:

1. By string literal

The string literal is created using single and double quotes. The syntax of creating a string using string literal is given below:

let myString = "string value";  


2. By string object using new keyword

The syntax of creating a string object using new keyword is given below. In this, the new keyword is used to create an instance of the string.

let myString = new String("Hello world");

JavaScript String Methods with Examples

1. length

It is used to find the length of a string.

Example:

let website = "Geeks Help";
console.log(website.length);

// Output: 10


2. toLowerCase()

It is used to convert the string to lowercase.

Example:

let website = "Geeks Help";
console.log(website.toLowerCase());

// Output: geeks help


3. toUpperCase()

It is used to convert the string to uppercase.

Example:

let website = "Geeks Help";
console.log(website.toUpperCase());

// Output: GEEKS HELP


4. charAt()

This method is used to get the character from a given string index.

Example:

let website = "Geeks Help";
console.log(website.charAt(6));

// Output: H


5. includes()

It is used to check whether the string includes the given character or not. If the string has given character then it will return true otherwise it will return false.

Example:

let website = "Geeks Help";
console.log(website.includes('Raju'));

// Output: false


6. concat()

The javascript contact() method is used to join two strings in a single string.

Example:

let website = "Geeks Help";
let author = " by Raju Webdev";
let output = website.concat(author);
console.log(output);

// Output: Geeks Help by Raju Webdev


7. endsWith()

It is used to check whether the string is endsWith a given character or not. If it returns with the given character then it will return true otherwise it will return false.

Example:

let website = "Geeks Help";
console.log(website.endsWith('p'));

// Output: true


8. replace()

This method is used to replace the first occurrence of the string.

Example:

let website = "geekshelp.in";
let output = website.replace('geekshelp', 'naukrinotice');
console.log(output);

// Output: naukrinotice.in


9. split()

This javascript string method is used to split the string into array.

Example:

let website = 'Geeks Help, Naukri Notice';
console.log(website.split(', '));

// Output: [ 'Geeks Help', 'Naukri Notice' ]


10. slice()

This method is used to print the string between given indexes.

Example:

let website = 'Geeks Help, Naukri Notice';
console.log(website.slice(12, 25));

// Output: Naukri Notice

11. indexOf()

It is used to find the index of the given character in a string. And it returns the first occurance.

Example:

let website = 'Geeks Help, Naukri Notice';
console.log(website.indexOf('G'));

// Output: 0


12. lastIndexOf()

It returns the index of the last occurrence of a substring within a string.

Example:

let website = 'Geeks Help';
console.log(website.lastIndexOf('e'));

// Output: 7


13. trim()

This method removes whitespace from both ends of a string and it returns a new string, without changing the original string.

Example:

let website = '       Geeks Help';
console.log(website.trim());

// Output: Geeks Help


14. charCodeAt()

This method provides the Unicode value of a character present at the specified index.

Example:

let myString = 'Hello';
let codeValue = myString.charCodeAt(4);
console.log(codeValue);

// Output: 111


15. search()

It searches a specified regular expression in a given string and returns its position if a match occurs. And if not match then it returns -1.

Example:

let myString = 'Geeks Help';
let index = myString.search('Help');
console.log(index);

// Output: 6


16. match()

It searches a specified regular expression in a given string and returns that regular expression if a match occurs. And if it doesn't match then it returns null.

Example:

let myString = "This the JavaScript 30 series";
let output = myString.match("JavaScript");

console.log(output)

// Output:
[
  'JavaScript',
  index: 9,
  input: 'This the JavaScript 30 series',
  groups: undefined
]


17. substr()

It is used to fetch the part of the given string on the basis of the specified starting position and length.

Example:

let myString = 'Geeks Help!';
let substring = myString.substr(7, 5);

console.log(substring);

// Output: elp!


18. valueOf()

The valueOf() javascript string method provides the primitive value of the string object.

Example:

let myString = 'Hello, World!';
let primitiveValue = myString.valueOf();
console.log(primitiveValue);

// Output: Hello, World!



Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad