JavaScript String Data Type
The String
data type in JavaScript is used to represent textual data. Strings can be created using single quotes, double quotes, or backticks (template literals). Strings are immutable, meaning that once created, their values cannot be changed.
Creating Strings
Strings can be created in two ways:
- Using string literals:
let str = 'DeathCode';
- Using the
String
constructor:let str2 = new String("DeathCode");
Example Code
let str = 'DeathCode'; // String literal
let str2 = new String("DeathCode"); // String object
// console.log(str.__proto__); // Outputs: String.prototype (shows the prototype of the string)
// console.log(str[7]); // Outputs: "o" (character at index 7)
Explanation
str
is a string literal, whilestr2
is a string object created using theString
constructor.str.__proto__
accesses the prototype of the string, which contains methods and properties available to all string instances.str[7]
retrieves the character at index 7 of the string, which is "o".
Common String Methods
JavaScript provides various methods to manipulate strings:
Example Code
let username = " username,email "; // String with extra spaces
username = username.trim(); // Removes whitespace from both ends
// console.log(username.toUpperCase()); // Outputs: "USERNAME,EMAIL"
// console.log(username.indexOf('n')); // Outputs: 3 (index of first occurrence of 'n')
// console.log(username.charAt(4)); // Outputs: "e" (character at index 4)
// console.log(username.replace("@#","$")); // Outputs: " username,email " (no match found)
// console.log(username.replaceAll("@","$")); // Outputs: " username,email " (no match found)
// console.log(username.slice(1,6)); // Outputs: "usern" (substring from index 1 to 5)
console.log(username.split("")); // Outputs: ["u", "s", "e", "r", "n", "a", "m", "e", ",", "e", "m", "a", "i", "l"]
Explanation
trim()
removes whitespace from both ends of the string.toUpperCase()
converts the string to uppercase.indexOf('n')
returns the index of the first occurrence of the character 'n'.charAt(4)
returns the character at index 4.replace()
replaces the first occurrence of a specified substring with a new substring, but in this case, there was no match found.replaceAll()
replaces all occurrences of a specified substring, but again, there was no match found.slice(1, 6)
extracts a section of the string from index 1 to index 5 (not inclusive).split("")
splits the string into an array of individual characters.
String Interpolation
JavaScript allows for string interpolation using template literals, which are enclosed by backticks.
Example Code
let a = 33, b = 55, c = 99;
console.log("a is: " + a + " b is: " + b + " c is: " + c); // Outputs: "a is: 33 b is: 55 c is: 99"
console.log(`a is ${a}, b is ${b}, c is ${c}`); // Outputs: "a is 33, b is 55, c is 99"
Explanation
- The first
console.log()
uses string concatenation to combine strings and variables. - The second
console.log()
uses template literals for easier and more readable string interpolation.
Conclusion
The String
data type is fundamental in JavaScript for handling text. Understanding how to create and manipulate strings is essential for effective programming.
Playlist of JS-Journey (JavaScript)
1. Introduction to JavaScript - What, Why, and How? | JavaScript Journey | In Hindi | Death CodeWatch
2. Variables & Comments in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
3. Understanding Data Types in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
4. Master Type Conversion & Reference Data Types π | JavaScript Journey | In Hindi | Death CodeWatch
5. Mastering Operators in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
6. Mastering Numbers & Math Functions π | JavaScript Journey | In Hindi | Death CodeWatch
7. Strings and String Methods in JavaScript π | JavaScript Journey | In Hindi | Death CodeCurrently Playing
8. Mastering Dates and Date Methods in JavaScript β³ | JavaScript Journey | In Hindi | Death CodeWatch
9. JavaScript Arrays & Array Methods Explained β³ | JavaScript Journey | In Hindi | Death CodeWatch
10. Objects and Object Methods Explained π | JavaScript Journey | In Hindi | Death CodeWatch
11. Conditional Statements | If, Switch & Ternary Operator | JavaScript Journey | In Hindi | Death CodeWatch
12. Mastering Loops (for, while, do-while) | JavaScript Journey | In Hindi | Death CodeWatch
13. Understanding Functions | JavaScript Journey | In Hindi | Death CodeWatch
14. HOFs, Callback Functions, Arrow Functions & More! π | JavaScript Journey | In Hindi | Death CodeWatch
15. Mastering Scope & Hoisting Explained in Hindi π₯ | JavaScript Journey | In Hindi | Death CodeWatch
16. Map, Filter, Reduce & forEach Explainedπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
17. Understanding this Keyword, Function Context & IIFEπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
18. Dive into Call Stack & JavaScript Execution Contextπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
19. Master Async Await & Event Loop π₯ | JavaScript Journey | In Hindi | Death CodeWatch
20. Promises in JavaScript Made Easy π | JavaScript Journey | In Hindi | Death CodeWatch
21. Fetch Function in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
22. JavaScript OOP Basics π | JavaScript Journey | In Hindi | Death CodeWatch
23. Mastering Prototypes & Inheritance in JS π | JavaScript Journey | In Hindi | Death CodeWatch
24. JavaScript Classes & Inheritance Explained! π | JavaScript Journey | In Hindi | Death CodeWatch
25. Mastering Getter and Setter (Why & How to use them?) π | JavaScript Journey | In Hindi | Death CodeWatch
26. JavaScript Property Descriptors π | JavaScript Journey | In Hindi | Death CodeWatch
27. DOM Basics π | JavaScript Journey | In Hindi | Death CodeWatch
28. How to Create, Update, and Delete DOM Elements π | JavaScript Journey | In Hindi | Death CodeWatch
29. JavaScript Events & Propagation π | JavaScript Journey | In Hindi | Death CodeWatch