Strings and String Methods in JavaScript πŸš€ | JavaScript Journey | In Hindi | Death Code - DeathCode

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, while str2 is a string object created using the String 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.

Β© 2024 DeathCode. All Rights Reserved.