String JS
String JS
Concatenation:
let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName; // Concatenation
String Length:
let text = "Hello, World!";
let length = text.length; // Length of the string (number
of characters)
Accessing Characters:
let text = "Hello, World!";
let firstChar = text[0]; // Accessing the first character
let lastChar = text[text.length - 1]; // Accessing the
last character
Substring:
let text = "Hello, World!";
let substring = text.substring(0, 5); // Extracts characters
from index 0 to 4 ("Hello")
String Methods:
JavaScript provides several string methods for manipulation,
including toUpperCase(), toLowerCase(), indexOf(), replace(),
etc.
let text = "Hello, World!";
let upperCaseText = text.toUpperCase(); // Converts to
uppercase
let lowerCaseText = text.toLowerCase(); // Converts to
lowercase
let indexOfComma = text.indexOf(","); // Finds the index of a
substring
let replacedText = text.replace("Hello", "Hi"); // Replaces a
substring
Template Literals:
Template literalsprovide a more convenient way to
concatenate strings and include variables.
let name = "John";
let greeting = `Hello, ${name}!`; // Using template literals