-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
Strings are one of the most fundamental data types in JavaScript. A string is a sequence of characters used to represent text. Strings are used everywhere: displaying messages to users, storing names, handling input from forms, processing data, working with APIs, and even building dynamic content for web pages. Understanding how strings work and how to manipulate them effectively is essential for any JavaScript developer.
In this chapter, you will learn what strings are, how to create them, basic operations, practical examples, common mistakes, best practices, and real-world applications.
A string in JavaScript is a sequence of characters enclosed in either single quotes ('), double quotes ("), or backticks (`). Strings can contain letters, numbers, symbols, spaces, and even special characters like emojis.
let name = "Ananya";
let greeting = 'Hello, Riya!';
let template = `Welcome, Mira!`;
Strings are immutable, which means that once created, the characters in a string cannot be changed directly. Any modification creates a new string.
Strings are essential because they allow developers to:
Display text and messages in the UI
Store and manipulate user input
Build dynamic content using concatenation or templates
Interact with APIs and databases
Process textual data like names, addresses, or emails
Perform operations such as searching, splitting, or formatting text
Almost every web application relies heavily on strings for communication, display, and data storage.
There are three ways to create strings in JavaScript:
let city = 'Patna';
let country = "India";
Template literals use backticks and allow embedded expressions and multi-line strings.
let student = "Mira";
let age = 22;
let message = `Hello, ${student}. You are ${age} years old.`;
console.log(message);
Concatenation combines two or more strings into one. You can use the + operator or template literals.
let firstName = "Riya";
let lastName = "Sharma";
let fullName = firstName + " " + lastName;
console.log(fullName);
let greeting = `Hi ${firstName}, welcome!`;
console.log(greeting);
The .length property returns the number of characters in a string, including spaces and symbols.
let studentName = "Sneha";
console.log(studentName.length); // 5
You can access individual characters in a string using square brackets [] with an index. Indexing starts from 0.
let name = "Kavya";
console.log(name[0]); // K
console.log(name[3]); // y
You can also use the .charAt() method:
console.log(name.charAt(2)); // v
Once a string is created, it cannot be changed directly. Any modification returns a new string.
let name = "Ananya";
let newName = name.replace("Ananya", "Diya");
console.log(name); // Ananya
console.log(newName); // Diya
let student1 = "Riya";
let student2 = "Mira";
let student3 = "Ananya";
let greetingMessage = `Hello ${student1}, ${student2}, and ${student3}! Welcome to the class.`;
console.log(greetingMessage);
let names = ["Sneha", "Kavya", "Diya"];
names.forEach(name => {
console.log(`${name} has ${name.length} characters.`);
});
let student = "Ishita";
console.log(`First character: ${student[0]}`);
console.log(`Last character: ${student[student.length - 1]}`);
let firstName = "Ananya";
let lastName = "Patel";
let fullName = `${firstName} ${lastName}`;
console.log(fullName);
let message = `Dear Riya,
Welcome to the programming course.
We hope you enjoy learning JavaScript!`;
console.log(message);
Using numbers or undefined variables directly in strings without conversion
Forgetting that strings are immutable and trying to change a character directly
Confusing single, double, and backtick quotes
Incorrectly using string concatenation leading to extra spaces or missing characters
Using index out of bounds while accessing characters
Use template literals for dynamic content
Always validate string input from users
Avoid unnecessary concatenation when using templates
Handle empty strings gracefully
Keep strings readable and well-formatted for multi-line messages
Strings are widely used in:
User interfaces and greetings
Storing names, addresses, emails, and passwords
API request and response processing
Dynamic content generation in websites
Logging and debugging messages
Reports, alerts, and notifications
Strings are the backbone of textual data in JavaScript. They allow you to store, display, and manipulate text efficiently. Using concatenation, template literals, and various string properties, you can create dynamic, readable, and user-friendly content. Mastering strings is essential for working with user input, web content, and almost every type of JavaScript application.
Q1. How do you find the length of the string "JavaScript"?
Q2. How do you convert the string "hello" to all uppercase letters using a built-in method?
Q3. How do you extract the substring "ava" from the string "JavaScript"?
Q4. How do you check whether a string "apple" includes the letter "p"?
Q5. How do you replace "good" with "awesome" in the string "good job"?
Q6. How do you convert "HELLO" to lowercase using a string method?
Q7. What method returns the position of the first occurrence of "a" in "banana"?
Q8. How do you split "red,green,blue" into an array of words?
Q9. How do you remove leading and trailing spaces from the string " hello "?
Q10. How do you create a greeting like "Hello, John!" using template literals with variable name = "John"?
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
