What Is a String?
A string is a sequence of characters that include letters, numbers, symbols, and spaces. A string is often wrapped in quotes, which let code Think the value is text without treating it as actual math. For example, “34” is a string and text, while 42 is a number; Order Strings stay connected together to form a message, look like a username such as “max_coder,” or contain mixed content. This distinction affects what happens when a program reads or processes data.
How Do You Create Strings in JavaScript?
JavaScript create Strings by using three types of quotes.
- Double quotes are the most common choice. Example: “Hello”.
- Single quotes works the same as double quotes for creating strings. Example: ‘Hello’.
- Backticks support template literals and special features such as embedding expressions. Example: `Hello, ${name}`.
Template Literals
Template literals use backticks (`) to create strings. They allow you to add variables directly inside a string using ${}.
let product = "Laptop";
let price = 500;
// Add variables inside a string
let message = `The ${product} costs $${price}.`;
console.log(message);
// "The Laptop costs $500."
Strings hold text. Join them with
Strings
+, measure with .length, and convert case with built-in methods.
String Length
The .length property of a string tells you how many characters the string contains.
Examples: let text1 = "Hello"; console.log(text1.length); // Output: 5 let text1 = "Hello"; console.log(text1.length); // Output: 5 let text3 = "12345"; console.log(text3.length); // Output: 5
Combining Strings
Concatenation
Concatenation is the process of joining two or more strings together to form a single string using the + operator.
let firstName = "John"; let lastName = "Doe"; let fullName = firstName + " " + lastName; console.log(fullName); // Output: John Doe let greeting = "Hello" + " " + "World!"; console.log(greeting); // Output: Hello World!
Essential String Methods
Letter Case Conversion
Changes a string to uppercase or lowercase letters.
JavaScript let text = "hello"; console.log(text.toUpperCase()); // Output: HELLO Removing Extra Spaces
Removes extra spaces from the beginning and end of a string.
JavaScript let text = " Hello World "; console.log(text.trim()); // Output: "Hello World"
Searching and Validating Text
Helps you find text or check if a string contains certain characters.
JavaScript let text = "JavaScript"; console.log(text.includes("Script")); // Output: true Getting Substrings
Gets a specific part of a string.
JavaScript let text = "Hello World"; console.log(text.slice(0, 5)); // Output: Hello Modifying Text Content
Replaces part of a string with new text.
JavaScript let text = "I like cats"; console.log(text.replace("cats", "dogs")); // Output: I like dogs Breaking and Combining Strings
Splits a string into parts or joins parts together.
JavaScript let text = "apple,banana,orange"; console.log(text.split(",")); // Output: ["apple", "banana", "orange"]
Strings Are Immutable
A String is Immutable, so String methods do not change the original string; they return a new string instead. If you want to keep the updated result, you need to save the returned new string. Example: let word = “code”; let updated = word.toUpperCase(); keeps word as “code” and stores “CODE” in updated.
Special Characters
Certain characters require special escape codes to be included in strings
// New line break let message = "Hello\nWelcome back!"; // Horizontal tab let info = "Age:\t25"; // Double quote inside a string let sentence = "He said \"Good morning.\""; // Single quote inside a string let contraction = 'Don\'t worry about it.'; // Backslash character let folder = "D:\\Projects\\JavaScript";
Frequently Used String Operations
- Validate Email (Simple): Check if a string looks like a valid email format →
- function containsAtSymbol(address) {
- return address.indexOf(“@”) !== -1;
- }
- containsAtSymbol(“hello@mail.com”); // true
containsAtSymbol(“hellomail.com”); // false - Capitalize First Letter: Convert the first character of a string to uppercase → let result = “hello”.charAt(0).toUpperCase() + “hello”.slice(1);
- Count Words: Calculate the number of words in a sentence → let count = “JavaScript is fun”.trim().split(“s”).length;
Strings Quiz Strings Quiz
Learn how text is stored, combined, and manipulated.
0%
What are the key takeaways about string handling?
- Use template literals with backticks for embedding variables, embedding multi-line strings, and to combine strings.
- Strings are sequences of characters, and characters are wrapped in quotes to represent text values.
- The + operator and template literals combine strings, while .length tells the characters count in a string.
- Strings have methods including toUpperCase(), slice(), and includes(), which perform common text operations.
- String methods return new strings and do not modify the original string, making these literals useful for text processing.