Strings in JavaScript
Coding with JavaScript
🕑 This lesson will take about 10 minutes
In this lesson, you will learn some different tasks you can perform with strings including:
how to concatenate (join) strings together
how to convert from integer to string
how to convert text to uppercase or lowercase letters
how to extract characters or portions of a string
indexing (accessing individual characters in a string)
how to split a string into different parts
Watch the videos below and scroll down for the sample code.
The sample code snippet below shows how to concatenate strings (join string values together), find the length of a string (the number of characters in a string), convert a value from integer to string data type, display specific characters in a string, split a string up and place each split component (eg. words) in a separate array element, and convert letters in a string value to uppercase or lowercase letters.
These string methods might not seem that useful at the moment, but they will likely come in very handy later. For example, imagine you are creating an app and users need to create a password for logging in. You might want to know the length of their password (stored as a string) to check it meets the minimum length for security requirements, or you might want to make sure a user’s phone number is not too long, or you might want to format someone’s name like changing the first letter of their first name and surname to uppercase.
Sample HTML & JavaScript code
Here is the sample HTML & JavaScript code. Note: You may need to open the codepen snippet in a new tab/window and click the Console button to view the output of the console.log() statements.
Next lesson: Event and event listeners