Strings are one of the most commonly used data types in JavaScript. A string represents a sequence of characters such as letters, numbers, symbols, and spaces.
Almost every web application works with text data. User names, passwords, messages, search keywords, product descriptions, and website content are all handled using strings.
JavaScript provides many built-in features to create, modify, search, and manipulate strings efficiently.
A string is a collection of characters enclosed inside single quotes, double quotes, or backticks.
let name = "Rahul"; let city = 'Delhi'; let message = `Welcome`;
All three values are strings in JavaScript.
Strings are essential because most programs need to process text information.
JavaScript provides three different ways to create strings.
Strings can be created by placing text inside double quotation marks.
let language = "JavaScript"; document.write(language);
JavaScript
Single quotes can also be used to create strings.
let course = 'Web Development'; document.write(course);
Web Development
Template literals use backticks (`) and allow embedding variables directly inside strings.
let name = "Amit";
let message = `Hello ${name}`;
document.write(message);
Hello Amit
When a string contains quotation marks, escape characters can be used.
let text = "JavaScript is \"easy\""; document.write(text);
JavaScript is "easy"
Escape characters are special characters used to represent characters that cannot be directly written inside a string.
| Escape Character | Description |
|---|---|
| \' | Single Quote |
| \" | Double Quote |
| \\ | Backslash |
| \n | New Line |
| \t | Tab Space |
The length property returns the total number of characters present inside a string.
let text = "JavaScript"; document.write(text.length);
10
Characters inside a string can be accessed using index numbers. String indexing starts from zero.
let language = "JavaScript"; document.write(language[0]);
J
| Character | Index |
|---|---|
| J | 0 |
| a | 1 |
| v | 2 |
| a | 3 |
| S | 4 |
Strings in JavaScript are immutable, which means individual characters cannot be directly changed.
let name = "Rahul"; name[0] = "M"; document.write(name);
Rahul
A new string must be created if changes are required.
String concatenation means joining two or more strings together.
let first = "Java"; let second = "Script"; let result = first + second; document.write(result);
JavaScript
let first = "Hello "; let second = "World"; let result = first.concat(second); document.write(result);
Hello World
JavaScript provides many built-in string methods that help developers perform different operations on text data. These methods make it easier to search, modify, extract, and format strings.
String methods do not change the original string because strings are immutable. Instead, they return a new string with the required changes.
The charAt() method returns the character present at a specific index position in a string.
string.charAt(index);
let text = "JavaScript"; document.write(text.charAt(4));
S
The charCodeAt() method returns the Unicode value of a character at a specified position.
let text = "A"; document.write(text.charCodeAt(0));
65
The includes() method checks whether a string contains a specific word or character.
It returns true if the value exists; otherwise, it returns false.
let message = "Welcome to JavaScript";
document.write(message.includes("JavaScript"));
true
The indexOf() method returns the first position of a specified value inside a string.
let text = "Programming";
document.write(text.indexOf("g"));
3
If the value is not found, the method returns -1.
The lastIndexOf() method returns the last occurrence position of a specified value.
let text = "JavaScript";
document.write(text.lastIndexOf("a"));
3
The startsWith() method checks whether a string begins with a specific value.
let text = "JavaScript";
document.write(text.startsWith("Java"));
true
The endsWith() method checks whether a string ends with a specific value.
let text = "JavaScript";
document.write(text.endsWith("Script"));
true
The toUpperCase() method converts all characters of a string into uppercase letters.
let text = "javascript"; document.write(text.toUpperCase());
JAVASCRIPT
The toLowerCase() method converts all characters of a string into lowercase letters.
let text = "JAVASCRIPT"; document.write(text.toLowerCase());
javascript
The trim() method removes extra spaces from the beginning and end of a string.
let text = " Hello World "; document.write(text.trim());
Hello World
The trimStart() method removes spaces only from the beginning of a string.
let text = " JavaScript"; document.write(text.trimStart());
JavaScript
The trimEnd() method removes spaces from the end of a string.
let text = "JavaScript "; document.write(text.trimEnd());
JavaScript
The slice() method extracts a part of a string and returns a new string.
string.slice(start,end);
let text = "JavaScript"; document.write(text.slice(0,4));
Java
The substring() method extracts characters between two indexes.
let text = "Programming"; document.write(text.substring(0,7));
Program
| slice() | substring() |
|---|---|
| Supports negative indexes. | Does not support negative indexes. |
| Can extract from end using negative values. | Treats negative values as zero. |
| Works with strings and arrays. | Mainly used with strings. |
The substr() method extracts a part of a string using starting position and length.
let text = "JavaScript"; document.write(text.substr(4,6));
Script
The substr() method is considered an older method. Modern applications generally prefer slice() or substring().
The replace() method replaces a specific value with another value in a string.
let text = "I like Java";
let result = text.replace("Java","JavaScript");
document.write(result);
I like JavaScript
The replaceAll() method replaces all occurrences of a value inside a string.
let text = "Java Java Java";
let result = text.replaceAll("Java","Python");
document.write(result);
Python Python Python
The split() method converts a string into an array based on a separator.
let text = "HTML,CSS,JavaScript";
let result = text.split(",");
document.write(result[1]);
CSS
The repeat() method creates a new string by repeating an existing string multiple times.
let text = "Hi "; document.write(text.repeat(3));
Hi Hi Hi
JavaScript allows comparison between strings using comparison operators.
let a = "JavaScript"; let b = "JavaScript"; document.write(a == b);
true
Template literals are a modern way to create strings in JavaScript. They were introduced in ES6 and use backticks (`) instead of single or double quotes.
Template literals make string creation easier because they support variable insertion, expressions, and multiline text.
let name = "Rahul";
let message = `Hello ${name}`;
document.write(message);
Hello Rahul
String interpolation means inserting variables or expressions directly inside a string using the ${ } syntax.
It is only supported inside template literals.
let name = "Amit";
let age = 20;
let result = `My name is ${name} and my age is ${age}`;
document.write(result);
My name is Amit and my age is 20
Template literals can execute JavaScript expressions directly inside strings.
let a = 10;
let b = 20;
let result = `Sum = ${a+b}`;
document.write(result);
Sum = 30
Traditional strings require escape characters for multiple lines, but template literals allow multiline strings directly.
let message = `Welcome to JavaScript Tutorial`; document.write(message);
Welcome to JavaScript Tutorial
JavaScript allows converting different data types into strings using built-in methods.
The String() method converts any value into a string.
let number = 123; let result = String(number); document.write(typeof result);
string
The toString() method converts numbers, arrays, and objects into strings.
let number = 500; document.write(number.toString());
500
JavaScript provides methods to convert numeric strings into numbers.
let value = "100"; let result = Number(value); document.write(result + 50);
150
The parseInt() method converts a string into an integer number.
let value = "250"; let result = parseInt(value); document.write(result);
250
The parseFloat() method converts a string containing decimal values into a floating-point number.
let price = "99.50"; let result = parseFloat(price); document.write(result);
99.5
JavaScript provides different methods to check whether specific text exists inside a string.
| Method | Description |
|---|---|
| includes() | Checks whether text exists. |
| startsWith() | Checks starting characters. |
| endsWith() | Checks ending characters. |
| indexOf() | Returns position of text. |
| search() | Searches using patterns. |
The search() method searches a string for a specified value or regular expression.
let text = "Learn JavaScript";
document.write(text.search("JavaScript"));
6
The match() method searches a string and returns matching results based on a pattern.
let text = "JavaScript is powerful";
let result = text.match("JavaScript");
document.write(result);
JavaScript
Regular expressions are patterns used to search and manipulate text.
They are commonly used for email validation, password checking, and searching complex patterns.
let text = "JavaScript123"; let result = text.match(/[0-9]+/); document.write(result);
123
let text = "JavaScript";
let reverse = text.split("")
.reverse()
.join("");
document.write(reverse);
tpircSavaJ
let text = "Programming"; document.write(text.length);
11
let text = "madam";
let reverse = text.split("")
.reverse()
.join("");
if(text == reverse)
{
document.write("Palindrome");
}
else
{
document.write("Not Palindrome");
}
Palindrome
let text = "JavaScript";
let count = 0;
for(let char of text)
{
if("aeiouAEIOU".includes(char))
{
count++;
}
}
document.write(count);
3
Strings are used in almost every web application for handling text-based information.
JavaScript strings are used to store and manipulate text data. They provide powerful methods for searching, modifying, formatting, and processing information.
Important string concepts include string creation, indexing, concatenation, template literals, conversion, searching methods, replacement methods, and regular expressions.
A strong understanding of strings helps developers build interactive websites, validate user data, process information, and work efficiently with modern JavaScript applications.