Loading...
Loading...
00:00:00

String in Javascript

String Syntax

In javascript string is zero or more characters inside single quotes or double quotes. we can declare a string variable without assigning the string into it.

let str0 = "";   // blank string
let str1 = "This is string in double quotes";  //string in double quotes
let str2 = 'This is string in single quotes';  // string in single quotes

String Escape Sequence Characters

In string we cannot write some special characters. For using special characters we have to use these escape sequence characters. Some important string sequence characters are listed here.

Escape character Output
\0 Null Byte
\n New Line
\t Horizontal Tabulator
\b Backspace
\f Form Feed
\r Carriage Return
\v Vertical Tabulator
\' Single quote
\" Double quote
\\ Backslash
How to use escape sequences characters. bellow is some Examples. we can use single quotes in double quotes similarly double quotes inside single quotes.
let str = "This is string first line 
This is string in second line.";   // new line
let str1 = "	This is string with horizontal tab.";   // horizontal tabulatorr
let str2 = ""Double quotes in string"";   // double quotes 
let str3 = "This is string with backslash ";   // backslash
console.log(str);
console.log(str1);
console.log(str2);
console.log(str3);

String Vs String Object

string and string object is different.

 let str1 = "Hello World";   //string variable
let str2 = new String("Hello World");    //string object 
console.log(str1 === str2);    //return false

String functions (methods)

Here is a comprehensive list of all the string methods available in JavaScript:

  1. charAt() - returns the character at a specified index in a string.
  2. charCodeAt() - returns the Unicode value of the character at a specified index in a string.
  3. concat() - joins two or more strings and returns a new string.
  4. includes() - checks whether a string contains a specified value, and returns true or false.
  5. endsWith() - checks whether a string ends with a specified value, and returns true or false.
  6. indexOf() - returns the position of the first occurrence of a specified value in a string.
  7. lastIndexOf() - returns the position of the last occurrence of a specified value in a string.
  8. localeCompare() - compares two strings in the current locale and returns a number indicating their relative order.
  9. match() - searches a string for a specified value and returns an array of matches.
  10. repeat() - returns a new string with a specified number of copies of an existing string.
  11. replace() - replaces a specified value in a string with another value.
  12. search() - searches a string for a specified value and returns the position of the match.
  13. slice() - extracts a part of a string and returns a new string.
  14. split() - splits a string into an array of substrings.
  15. startsWith() - checks whether a string starts with a specified value, and returns true or false.
  16. substr() - extracts a specified number of characters from a string, starting at a specified index.
  17. substring() - extracts the characters between two specified indices in a string.
  18. toLocaleLowerCase() - converts a string to lowercase, according to the current locale.
  19. toLocaleUpperCase() - converts a string to uppercase, according to the current locale.
  20. toLowerCase() - converts a string to lowercase.
  21. toString() - returns the string representation of an object.
  22. toUpperCase() - converts a string to uppercase.
  23. trim() - removes whitespace from both ends of a string.
  24. valueOf() - returns the primitive value of an object.

These methods are built-in to the String object in JavaScript, and can be used to manipulate and work with strings in a wide variety of ways.

How to Use String Functions 

Length

length string property return the number of characters in a string.

let str = "pandit programmer";   //string variable
console.log(str.length);    // return 17

ToLowerCase()

toLowerCase() string method is convert a string into lowercase and return it.

let str = "Pandit Programmer";  
console.log(str.toLowerCase());  // return - pandit programmer