Creating a String
let greeting = 'Hello World!';
let company = "Acme Corp";
let address = '123 Main Street';
You can also concatenate strings by using the + operator:
let firstName = 'John';
let lastName = 'Doe';
let fullName = firstName + ' ' + lastName;
// Output: "John Doe"
Accessing Characters in a String
To access individual characters within a string, you can use square brackets  and provide the desired index number. For example:
alert(str); // Output: "J"
alert(str); // Output: "S"
The length of a string is the number of characters it contains. You can retrieve the length of a string by using the length property:
let message = 'Hello, World!';
alert(message.length); // Output: 13
Common String Methods
- toUpperCase(): Converts a string to uppercase.
- toLowerCase(): Converts a string to lowercase.
- trim(): Removes whitespace from both ends of a string.
- substring(startIndex, endIndex): Extracts a portion of a string between the specified start and end index (end index not included).
- replace(oldValue, newValue): Replaces occurrences of a specified value with another value in a string.
- charAt(index): Returns the character at the specified index.
Here’s an example that demonstrates some of these methods: