String Manipulation:
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
These examples cover some fundamental math operations
using the Math object and various string manipulations in
JavaScript. Understanding these concepts is essential for
working with numerical and string data in your JavaScript
programs.