Lesson 11 of 30 – JavaScript Strings
33%

JavaScript Strings

A JavaScript string is a primitive data type used to store text. Strings can be written using single quotes (' '), double quotes (" "), or backticks (` `).

Note: Strings are used to store and manipulate textual data in JavaScript.
What is a String?

A string is a sequence of characters enclosed in quotes.

Examples:
"Hello World"
'Hello Soopro'
`Hello Pathshala`
Creating String Variables

Strings can be assigned to variables.

Example:
let str1 = "This is a double quoted string.";

let str2 = 'This is a single quoted string.';

let str3 = `This is a template string.`;
Template Strings

Template strings use backticks (` `) and allow variables or expressions to be inserted using ${ }.

Example:
let amount = 1000;
let rate = 0.05;
let duration = 3;

let result = `Total Amount Payable: ${amount*(1+rate*duration)}`;

Template strings can also span multiple lines.

Strings as Character Arrays

A string can be accessed like an array.

Example:
let str = "Hello World";

str[0]; // H
str[1]; // e
str.at(2); // l
str.at(3); // l

Strings are immutable, so characters cannot be changed directly.

str[4] = "P"; // Not allowed
Quotes Inside Strings

You can use single quotes inside double quotes and vice versa.

Example:
let str1 = "This is 'simple' string";

let str2 = 'This is "simple" string';

let str3 = `This is 'simple' and "easy" string`;
String Concatenation

Strings can be joined using the + operator or concat() method.

Example:
let str1 = "Hello ";
let str2 = "World";

let str3 = str1 + str2;

let str4 = str1.concat(str2);
String Objects

JavaScript allows you to create string objects using the new String() constructor.

Example:
let str1 = new String();
str1 = "Hello World";

let str2 = new String("Hello World");

String objects and string literals are different.

Example:
let str1 = new String("Hello World");
let str2 = "Hello World";

typeof(str1); // object
typeof(str2); // string
String Comparison

Two strings can be compared using comparison operators.

  • <
  • >
  • ==
  • ===
  • localeCompare()

The operators compare strings alphabetically.

Example:
"Apple" < "Banana";
"Cat" > "Ball";
"Hello" == "Hello";

The localeCompare() method compares strings according to the current language settings.

String Property

JavaScript strings have useful properties.

Property Description
length Returns the number of characters in a string.
Example:
let str = "Soopro";

str.length; // 6
Common String Methods
Method Purpose
charAt() Returns character at a position.
concat() Joins two or more strings.
indexOf() Finds first occurrence.
lastIndexOf() Finds last occurrence.
replace() Replaces text.
slice() Extracts part of a string.
split() Converts string to array.
substring() Extracts characters.
toLowerCase() Converts to lowercase.
toUpperCase() Converts to uppercase.
String Method Examples
let str = "Soopro Pathshala";

str.charAt(0); // S
str.indexOf("Path"); // 7
str.toUpperCase();
str.toLowerCase();
str.replace("Soopro","Hello");
str.slice(0,6);
Useful String Functions
Method Description
match() Searches using regular expressions.
search() Finds matching text.
substr() Extracts characters by length.
valueOf() Returns primitive value.
toString() Returns string representation.
localeCompare() Compares two strings.
Advantages of JavaScript Strings
  • Easy to store textual data.
  • Support powerful built-in methods.
  • Can be concatenated easily.
  • Template literals make formatting simple.
  • Useful for web development and user input.
  • Support searching and replacing text.
Key Points
  • Strings are primitive data types.
  • Strings can use single quotes, double quotes, or backticks.
  • Template literals support variable interpolation.
  • Strings behave like arrays of characters.
  • Strings are immutable.
  • The length property returns string size.
  • JavaScript provides many built-in string methods.
  • String objects and string literals are different.
Best Practices
  • Use string literals instead of String objects.
  • Prefer template literals for dynamic text.
  • Use meaningful variable names.
  • Use built-in methods instead of complex logic.
  • Avoid unnecessary string concatenation.
  • Keep code readable and maintainable.
Quick Revision
Concept Example
Single Quote 'Hello'
Double Quote "Hello"
Template Literal `Hello ${name}`
Length str.length
Uppercase str.toUpperCase()
Lowercase str.toLowerCase()
Concatenation str1 + str2

🧠 Quick Quiz

Which symbol is used for JavaScript template strings?