JavaScript - 17
JavaScript - 17: String Methods
Strings have built-in methods and properties that help you work with text.
Your challenge: Use the text variable to create an uppercase version, find its length, check for a word, and replace part of the text.
Convert text to uppercase
Use: text.toUpperCase()
This returns a new uppercase version of the string.
Find the string length
Use: text.length
The length includes letters, spaces, and punctuation.
Check whether text contains a word
Use: text.includes("JavaScript")
This returns either true or false.
Replace part of a string
Use: text.replace("JavaScript", "coding")
This creates a new version of the string with the selected word replaced.
Important: Methods such as .toUpperCase() and .replace() do not change the original string. They return a new string instead.
Try it: Change the original sentence and see how each result changes.
Small experiment: Try text.includes("learning"), then try the same word with different capitalization.
Good JavaScript habit: Remember that .length is a property, while methods such as .toUpperCase(), .includes(), and .replace() use parentheses.
👀 Show Solution
const text = "I love learning JavaScript!";
console.log("Uppercase:", text.toUpperCase());
console.log("Length:", text.length);
console.log("Contains JavaScript:", text.includes("JavaScript"));
console.log("Replaced:", text.replace("JavaScript", "coding"));The four lines use the same original string to produce different results without changing the value stored in text.
