String Manipulation Techniques
1234567891011121314151617// String concatenation const firstName = "Alex"; const greeting1 = "Hello, " + firstName + "!"; // Template literal const greeting2 = `Hello, ${firstName}!`; // Using string methods const shout = greeting2.toUpperCase(); const hasHello = greeting2.includes("Hello"); const messageLength = greeting2.length; console.log(greeting1); // Hello, Alex! console.log(greeting2); // Hello, Alex! console.log(shout); // HELLO, ALEX! console.log(hasHello); // true console.log(messageLength); // 12
When working with strings in JavaScript, you often need to combine, inspect, or modify them. You have seen two primary ways to build a greeting message: string concatenation using the + operator, and template literals using backticks and ${} for embedding variables. Template literals make it easier to include variables and expressions directly in strings.
Beyond building strings, you can use several helpful string methods. The length property tells you how many characters are in a string, including spaces and punctuation. The toUpperCase() method returns a new string with all letters converted to uppercase, which is useful for standardizing text. The includes() method checks if a substring exists within a string, returning true or false. For example, greeting2.includes("Hello") checks if "Hello" is part of the greeting message.
Combining these techniques allows you to construct and analyze strings efficiently, which is essential for tasks like formatting output, validating input, or searching within text.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 6.25
String Manipulation Techniques
Swipe to show menu
1234567891011121314151617// String concatenation const firstName = "Alex"; const greeting1 = "Hello, " + firstName + "!"; // Template literal const greeting2 = `Hello, ${firstName}!`; // Using string methods const shout = greeting2.toUpperCase(); const hasHello = greeting2.includes("Hello"); const messageLength = greeting2.length; console.log(greeting1); // Hello, Alex! console.log(greeting2); // Hello, Alex! console.log(shout); // HELLO, ALEX! console.log(hasHello); // true console.log(messageLength); // 12
When working with strings in JavaScript, you often need to combine, inspect, or modify them. You have seen two primary ways to build a greeting message: string concatenation using the + operator, and template literals using backticks and ${} for embedding variables. Template literals make it easier to include variables and expressions directly in strings.
Beyond building strings, you can use several helpful string methods. The length property tells you how many characters are in a string, including spaces and punctuation. The toUpperCase() method returns a new string with all letters converted to uppercase, which is useful for standardizing text. The includes() method checks if a substring exists within a string, returning true or false. For example, greeting2.includes("Hello") checks if "Hello" is part of the greeting message.
Combining these techniques allows you to construct and analyze strings efficiently, which is essential for tasks like formatting output, validating input, or searching within text.
Thanks for your feedback!