Basics

JS Strings

JavaScript String Methods

JavaScript strings are immutable, with methods like slice and indexOf.

Introduction to JavaScript Strings

In JavaScript, a string is a sequence of one or more characters that may consist of letters, numbers, or symbols. Strings in JavaScript are immutable, meaning once a string is created, it cannot be changed. Any operation that seems to modify a string will actually create a new string.

Creating Strings

Strings can be created using single quotes, double quotes, or backticks. Here are some examples:

String Immutability

When you attempt to change a character of a string, it does not alter the original string. Instead, you create a new string:

Common String Methods

JavaScript provides many methods to work with strings. Here are some of the most commonly used:

slice()

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.

indexOf()

The indexOf() method returns the index within the calling string of the first occurrence of the specified value, or -1 if not found.

Conclusion

Understanding strings is crucial for handling text in JavaScript. Remember, strings are immutable, and any method that appears to change a string will instead return a new one. Explore the various string methods to manipulate and analyze text efficiently.

Previous
Arrays