Introduction
In JavaScript, a string is a sequence of characters used to represent text. Strings are one of the fundamental data types in JavaScript and are commonly used to store and manipulate textual data.
String can be enclosed in Single Quotes like ‘john’ or Double Quotes like “john” or backtics (which came into ES6) like `john`.
What is a JavaScript String?
A String in JavaScript is a sequence of characters enclosed inside quotes. It can contain letters, numbers, symbols, and spaces.
JavaScript supports three ways to create strings:
- Single quotes (”)
- Double quotes (“”)
- Template literals ( )
Syntax of JavaScript String
let variableName = 'value';
//or
let variableName = "value";
//or
let variableName = `value`;
Example:
let name = 'John';
let city = "Delhi";
let message = `Welcome to JavaScript`;
Creating Strings in JavaScript
You can create a string in the following ways.
1. Using Double Quotes
let name = "John";
console.log(name);
Output:
2. Using Single Quotes
let city = 'Delhi';
console.log(city);
Output:
3. Using Template Literals
Template literals support variables and multiline strings.
let name = "John";
console.log(`Hello ${name}`);
Output:
4. Using String() Method
You can create a string through the String() function.
var age=35;
var age_string=String(age);
console.log(age_string); // Output:- "35"
Output:
5. Using toString() Method
toString() is used to convert data into a string.
var age =35;
var age_string=age.toString();
console.log(age_string); // Output:- "35"
Output:
6. Using a new keyword
Creating a String Object through the new keyword, and it behaves like an Object.
var string_object = new String("John");
console.log(string_object);
console.log(string_object.toString());
console.log(string_object.valueOf());
Output:
John
John
JavaScript String Methods
Strings provide built-in methods for manipulating text.
1. length Property
It returns the total number of characters.
let text = "John";
console.log(text.length);
Output:
2. toUpperCase() Method
It converts text into uppercase.
let name = "john";
console.log(name.toUpperCase());
Output:
3. toLowerCase() Method
It converts text into lowercase.
let city = "DELHI";
console.log(city.toLowerCase());
Output:
4. trim() Method
It removes extra spaces.
let str = " Hello ";
console.log(str.trim());
Output:
5. includes() Method
It checks whether text exists.
let msg = "Learn JavaScript";
console.log(msg.includes("Java"));
Output:
6. replace() Method
It replaces characters or words.
let text = "I like Java";
console.log(text.replace("Java","JavaScript"));
Output:
7. slice() Method
It extracts part of a string.
let word = "Hello World";
console.log(word.slice(0,4));
Output:
8. split() Method
It converts a string into an array.
let fruits = "Apple,Banana,Mango";
console.log(fruits.split(","));
Output:
How to Concatenate a String
Combining multiple strings is called concatenation.
1. Using + to add Multiple Strings
Example:
let firstName = "John";
let lastName = "Taylor";
console.log(firstName + " " + lastName);
Output:
2. Using template literals:
let first = "John";
let last = "Taylor";
console.log(`${first} ${last}`);
Output:
Escape Characters in Strings
Escape characters allow special formatting.
Example:
let text = "He said \"Hello\"";
console.log(text);
Output:
Common escape characters:
| Escape Character | Meaning | Example Output |
|---|---|---|
\n |
New Line | Moves text to next line |
\t |
Tab | Adds horizontal space |
\" |
Double Quote | Prints ” inside string |
\' |
Single Quote | Prints ‘ inside string |
\\ |
Backslash | Prints \ |
Example of JavaScript String
Example 1: Display User Name
let username = "John";
console.log("Welcome " + username);
Output:
Example 2: Find String Length
let course = "JavaScript";
console.log(course.length);
Output:
Example 3: Convert Uppercase
let name = "John";
console.log(name.toUpperCase());
Output:
Common Mistakes in JavaScript Strings
You will see some common mistakes in JavaScript Strings
Mistake 1: Forgetting Quotes
Wrong:
let name = John;
Error:
Correct:
let name = "John";
Mistake 2: Using the Wrong Case in Methods
Wrong:
text.touppercase();
Correct:
text.toUpperCase();
Note: JavaScript is case-sensitive.
Mistake 3: Mixing Quotes Incorrectly
Wrong:
let text = "It's good";
Correct:
let text = "It's good";
or
let text = 'It\'s good';
Mistake 4: Confusing Number and String
let age = "20";
console.log(age + 5);
Output:
Note: Because “20” is a string.
Correct:
console.log(Number(age)+5);
Output:
JavaScript String – Interview Questions
Q 1: What is a string?
Q 2: How to define a string?
Q 3: Are strings immutable?
Q 4: How to find the string length?
Q 5: How to convert to uppercase?
JavaScript String – Objective Questions (MCQs)
Q1. Strings in JavaScript are written inside ______.
Q2. Which method returns string length?
Q3. Which is a valid string?
Q4. Which method converts string to uppercase?
Q5. JavaScript strings are ______.
Conclusion
JavaScript strings are used to store and manipulate text data. They are essential in web development because websites constantly work with names, messages, form inputs, API responses, and dynamic content. Learning string methods like length(), slice(), replace(), split(), trim(), and includes() helps developers write efficient JavaScript code.
If you want to become good at JavaScript, understanding strings is one of the most important topics to master because almost every real-world project uses them.