A string is used to store text in JavaScript. For example, a person’s name, a message, or a city name can be stored as a string.
let name = "Aman";
let city = "Delhi";
console.log(name);
console.log(city);
Output:
Aman
Delhi
Creating Javascript Strings
We can create a string using single quotes, double quotes, or backticks.
let firstName = "Aman";
let lastName = 'Kumar';
let message = `Hello!`;
All three store text as strings.
String Length
The length property tells us how many characters are in a string.
let text = "JavaScript";
console.log(text.length);
Output:
10
Spaces are also counted as characters.
Accessing String Characters
Each character in a string has an index number. The first character starts at index 0.
let name = "Aman";
console.log(name[0]);
console.log(name[2]);
Output:
A
a
We can use the index to get a particular character from a string.
JavaScript String Methods
String methods are ready-made functions that help us work with strings.
toUpperCase()
It changes all letters to uppercase.
let text = "hello";
console.log(text.toUpperCase());
Output:
HELLO
toLowerCase()
It changes all letters to lowercase.
let text = "JAVASCRIPT";
console.log(text.toLowerCase());
Output:
javascript
trim()
The trim() method removes extra spaces from the beginning and end of a string.
let text = " Hello ";
console.log(text.trim());
Output:
Hello
includes()
The includes() method checks whether a string contains some text.
It returns true or false.
let text = "I love JavaScript";
console.log(text.includes("JavaScript"));
Output:
true
slice()
The slice() method takes a part of a string.
let text = "JavaScript";
console.log(text.slice(0, 4));
Output:
Java
The first number is the starting index, and the second number is where the extraction stops.
replace()
The replace() method replaces some text with other text.
let text = "I like Java";
console.log(text.replace("Java", "JavaScript"));
Output:
I like JavaScript
JavaScript String Search
indexOf()
The indexOf() method returns the position of the first occurrence of some text.
let text = "Hello JavaScript";
console.log(text.indexOf("JavaScript"));
Output:
6
If the text is not found, indexOf() returns -1.
lastIndexOf()
The lastIndexOf() method finds the position of the last occurrence of some text.
let text = "JavaScript is fun. JavaScript is useful.";
console.log(text.lastIndexOf("JavaScript"));
Output:
20
JavaScript Template Strings
Template strings use backticks instead of quotes. They make it easy to put variables inside a string using ${}.
let name = "Aman";
let age = 14;
let message = `My name is ${name} and I am ${age} years old.`;
console.log(message);
Output:
My name is Aman and I am 14 years old.
This is called string interpolation.
JavaScript Escape Characters
Escape characters allow us to use special characters inside strings. For example, \" allows us to use double quotes inside a string.
let message = "He said, \"Hello!\"";
console.log(message);
Output:
He said, "Hello!"
Another useful escape character is \n, which creates a new line.
let text = "Hello\nJavaScript";
console.log(text);
Output:
Hello
JavaScript
Quick Recap
- Strings are used to store text.
- Strings can use single quotes, double quotes, or backticks.
lengthcounts the characters in a string.- String characters use index numbers starting from
0. - Methods like
toUpperCase(),trim(),includes(),slice(), andreplace()help us work with strings. indexOf()andlastIndexOf()help find text inside a string.- Template strings make it easy to add variables inside text.
- Escape characters help us write special characters in strings.
Frequently Asked Questions (FAQs)
Q1. What are JavaScript Strings?
A JavaScript String is used to store and work with text, such as names, messages, or city names. Strings can be created using single quotes, double quotes, or backticks.
Q2. What are JavaScript String Methods?
JavaScript String Methods are built-in functions that help you perform different operations on text. Common methods include toUpperCase(), toLowerCase(), trim(), includes(), slice(), and replace().
Q3. What are commonly used JavaScript String Methods?
Common methods include toUpperCase(), toLowerCase(), trim(), includes(), slice(), and replace().
Q4. What are Template Strings in JavaScript?
Template Strings use backticks and allow variables to be inserted directly into text using ${}. This makes it easier to create dynamic messages using String Interpolation.
Q5. What are Escape Characters in JavaScript Strings?
Escape Characters are used to include special characters inside strings. For example, \" allows quotes inside a string, while \n is used to create a new line.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
