Lesson 5
Basic String Manipulation Features in JavaScript
Lesson Overview

Welcome! In this lesson, we'll delve into the basic string manipulation features of JavaScript, which include string tokenization, string concatenation, trimming of whitespace from strings, and type conversion operations.

Tokenizing a String in JavaScript

In JavaScript, we can use the split method from the String class to tokenize a string, essentially splitting it into smaller parts or 'tokens'.

JavaScript
1let sentence = "JavaScript is an amazing language!"; 2let tokens = sentence.split(" "); 3 4tokens.forEach(token => console.log(token)); 5 6// Output: 7// JavaScript 8// is 9// an 10// amazing 11// language!

We start by declaring a string variable sentence containing the text "JavaScript is an amazing language!". On the second line, we use the split method with a space character " " as the delimiter. This method splits the sentence every time it encounters a space and returns an array of substrings or tokens. In this case, the resulting tokens array will contain ["JavaScript", "is", "an", "amazing", "language!"]. We then use the forEach method to iterate over each element (token) in the tokens array. The arrow function token => console.log(token) is executed for each token, printing each word to the console, one per line.

Exploring String Concatenation

In JavaScript, the + operator or template literals can be used to concatenate strings into a larger string:

Using the + Operator:

JavaScript
1let str1 = "Hello,"; 2let str2 = " World!"; 3let greeting = str1 + str2; 4console.log(greeting); // Output: "Hello, World!"

Using Template Literals:

JavaScript
1let str1 = "Hello,"; 2let str2 = " World!"; 3let greeting = `${str1} ${str2}`; 4console.log(greeting); // Output: "Hello, World!"

You can also concatenate arrays of strings in JavaScript using the join method:

Using Array join Method:

JavaScript
1let strings = ["Hello", " World!", " JavaScript", " Arrays!"]; 2let result = strings.join(""); 3console.log(result); // Output: "Hello World! JavaScript Arrays!"

In the example above:

  1. Array Initialization: We initialize an array with several strings.
  2. Using the join Method: We use the join method to concatenate all the elements of the array into a single string. The join method can also take an optional delimiter as an argument if you need to insert characters (like commas or spaces) between the elements.
Trimming Whitespace from Strings

In JavaScript, the trim method can remove both leading and trailing whitespace from a string:

JavaScript
1let str = " Hello, World! "; // string with leading and trailing spaces 2str = str.trim(); // remove leading and trailing spaces 3console.log(str); // Output: "Hello, World!"

In this example, trim is used to remove leading and trailing whitespace from a string.

JavaScript Type Conversions

We can convert strings to numbers using methods like parseInt (string to integer) and parseFloat (string to float), and other data types to strings using String:

JavaScript
1let numStr = "123"; 2let num = parseInt(numStr); 3console.log(num); // Output: 123 4 5let floatStr = "3.14"; 6let pi = parseFloat(floatStr); 7console.log(pi); // Output: 3.14 8 9let age = 20; 10let ageStr = String(age); 11console.log("I am " + ageStr + " years old."); // Output: I am 20 years old.

In this code, we use parseInt, parseFloat, and String for type conversions.

Integrating String Tokenization and Type Conversions

In some cases, we may need to combine all the methods discussed:

JavaScript
1let numbers = "1,2,3,4,6"; 2let numArray = numbers.split(","); 3let sum = 0; 4 5numArray.forEach(numStr => { 6 sum += parseInt(numStr); 7}); 8 9let average = sum / numArray.length; 10console.log("The average is " + average); // Output: The average is 3.2

By integrating these methods, we can transform the string 1,2,3,4,6 into an array of integers, calculate their average, and display the result.

Quick Recap and Next Steps

Great job! You've gained an overview of JavaScript's string manipulation features, including string concatenation, string tokenization, trimming whitespace from strings, and type conversions. Now, it's time to get hands-on with these concepts in the exercises that follow. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.