Hello! Today, we're going to explore some key JavaScript string operations: concatenation, slicing, charAt
, includes
, indexOf
, toLowerCase
, and toUpperCase
. We'll unlock these skills through examples.
Strings in JavaScript are used to store and manage text. To create a string, you can use single quotes (' '
), double quotes (" "
), or backticks (``
). Here's an example:
JavaScript1let firstName = 'Tim'; // Using single quotes 2let lastName = "Cook"; // Using double quotes 3let job = `CEO of Apple Inc.`; // Using backticks 4 5console.log(firstName); // Prints: "Tim" 6console.log(lastName); // Prints: "Cook" 7console.log(job); // Prints: "CEO of Apple Inc."
In the example above, each of the variables firstName
, lastName
, and job
holds a string.
Concatenation combines strings. In JavaScript, we use the +
operator for this task. It's like merging puzzle pieces. Consider the following example:
JavaScript1let planet = 'Mars'; 2let welcomeMessage = 'Welcome to ' + planet + '!'; 3console.log(welcomeMessage); // Prints: "Welcome to Mars!"
In the example above, we've combined two strings to form a new string: 'Welcome to Mars!'.
Slicing involves extracting a substring (a part or segment of the string). The substring(from, to)
method in JavaScript facilitates this task - it returns the substring constructed from characters at positions from
, from + 1
, ..., to - 1
, i.e., you start from the character at the position from
, and end at to
, excluding it.
A version of this method substring(from)
, at the same time, just takes the substring from the provided character till the end.
Let's see how it works:
JavaScript1let message = "Hello, World!"; 2let slice = message.substring(1, 5); // A substring starting at character 1 to 5 (exclusive) 3console.log(slice); // Prints: "ello" 4 5// A substring starting at character 2, till the end of the string 6console.log(message.substring(2)); // Prints: "llo, World!" 7 8// A substring starting at character 7 to 13 (exclusive) 9console.log(message.substring(7, 13)); // Prints: "World!"
The charAt
method retrieves a character from a specific position within a string. Similar to lists, the position (index) counts from 0
. Here's how you use it:
JavaScript1let greeting = "Hello"; 2console.log(greeting.charAt(0)); // Prints: H 3console.log(greeting.charAt(1)); // Prints: e 4console.log(greeting.charAt(4)); // Prints: o
By using charAt(0)
, we've retrieved the first character of greeting
— 'H'.
The includes
and indexOf
methods are commonly used tools for exploring strings. includes
searches for a particular text within a string, while indexOf
returns its position, or -1
if there is no occurrence of the provided text:
JavaScript1let phrase = "The quick brown fox"; 2console.log(phrase.includes("fox")); // Prints: true, because "fox" is in phrase 3console.log(phrase.indexOf("fox")); // Prints: 16, the starting position of 'fox' in phrase 4 5console.log(phrase.includes("red")); // false 6console.log(phrase.indexOf("red")); // -1, because there is no occurrence of "red"
For converting all string letters to lowercase or uppercase, JavaScript provides the toLowerCase
and toUpperCase
methods respectively:
JavaScript1let message = "Hello World!"; 2console.log(message.toLowerCase()); // Prints: "hello world!" 3console.log(message.toUpperCase()); // Prints: "HELLO WORLD!"
The toLowerCase
and toUpperCase
methods convert strings to all lowercase and all uppercase, respectively.
Congratulations! You've learned how to use various JavaScript string operations: concatenation, slicing, charAt
, includes
, indexOf
, and case conversion. Now, it's time to master these skills through intriguing, hands-on practice exercises. Let's get started!