Here is a detailed, MCA-level explanation (website-ready) of
๐ The String Object in JavaScript
๐ง 1. Introduction to String Object
In JavaScript, a String is used to represent textual data.
A string can be:
- A primitive value
- An object created using the
Stringconstructor
โ Example:
let str1 = "Hello"; // String literal (primitive)
let str2 = new String("Hello"); // String object
๐ Generally, string literals are preferred over new String().
๐ 2. Characteristics of Strings
- Strings are immutable (cannot be changed after creation)
- Indexed (each character has a position)
- Supports many built-in methods
Example:
let str = "JavaScript";
console.log(str[0]); // J
๐งฉ 3. Creating Strings
(a) Using Quotes
let s1 = "Hello";
let s2 = 'World';
let s3 = `JavaScript`; // Template literal
(b) Using Constructor
let s4 = new String("JS");
โจ 4. String Properties
๐ length
Returns number of characters
let str = "Hello";
console.log(str.length); // 5
โ๏ธ 5. Important String Methods
๐ (1) charAt()
Returns character at a given index
let str = "JavaScript";
console.log(str.charAt(0)); // J
๐ (2) indexOf()
Returns first occurrence index
let str = "JavaScript";
console.log(str.indexOf("a")); // 1
๐ (3) lastIndexOf()
Returns last occurrence
console.log(str.lastIndexOf("a"));
๐ (4) includes()
Checks if substring exists
console.log(str.includes("Script")); // true
โ๏ธ (5) slice()
Extracts part of string
let str = "JavaScript";
console.log(str.slice(0, 4)); // Java
โ๏ธ (6) substring()
Similar to slice (no negative index)
console.log(str.substring(0, 4));
๐ (7) toUpperCase() / toLowerCase()
let str = "hello";
console.log(str.toUpperCase()); // HELLO
๐ (8) replace()
Replaces part of string
let str = "Hello World";
console.log(str.replace("World", "JS"));
๐ (9) concat()
Joins strings
let s1 = "Hello";
let s2 = "World";
console.log(s1.concat(" ", s2));
โ๏ธ (10) trim()
Removes extra spaces
let str = " Hello ";
console.log(str.trim());
๐ข (11) split()
Converts string into array
let str = "a,b,c";
console.log(str.split(","));
๐ค (12) startsWith() / endsWith()
let str = "JavaScript";
console.log(str.startsWith("Java")); // true
๐ 6. String Immutability
Strings cannot be changed directly.
let str = "Hello";
str[0] = "Y";
console.log(str); // Hello (unchanged)
๐ Instead, create a new string:
str = "Y" + str.slice(1);
๐งฎ 7. Template Literals
Used for dynamic strings
let name = "John";
let msg = `Hello ${name}`;
console.log(msg);
โ ๏ธ 8. Difference: Primitive vs Object
| Feature | Primitive String | String Object |
|---|---|---|
| Type | string | object |
| Syntax | “Hello” | new String(“Hello”) |
| Performance | Faster | Slower |
| Usage | Recommended | Avoid |
๐ 9. Complete Example
let str = " JavaScript ";
console.log(str.length);
console.log(str.trim());
console.log(str.toUpperCase());
console.log(str.includes("Script"));
console.log(str.slice(1, 5));
console.log(str.replace("Java", "Type"));
๐ Summary Table
| Method | Purpose |
|---|---|
| charAt() | Get character |
| indexOf() | Find position |
| slice() | Extract part |
| replace() | Replace text |
| toUpperCase() | Uppercase |
| trim() | Remove spaces |
| split() | Convert to array |
๐ฏ Conclusion
The String Object is essential in JavaScript for:
- Handling text data
- Performing string manipulation
- Building dynamic web applications
๐ Mastery of string methods is crucial for real-world programming and MCA exams.
