Strings are used whenever JavaScript needs to work with text such as names, messages, email addresses, product names, and sentences. JavaScript provides many built-in string methods for finding text, changing case, extracting characters, replacing words, and checking string content. In this chapter, you will practice the most useful string operations through simple examples. JavaScript Strings and String Methods Practice Questions with Solutions that gradually build your understanding.
Question 1: Create and Display a String
Problem
Create a variable named message, store "Welcome to JavaScript" in it, and display the message.
Solution
letmessage="Welcome to JavaScript";console.log(message);
Output
Welcome to JavaScript
Step-by-step Explanation
messageis a variable used to store text."Welcome to JavaScript"is a string.- The text is enclosed in double quotation marks.
console.log()displays the string in the console.
Question 2: Find the Length of a String
Problem
Create a variable containing "JavaScript" and find the number of characters in the string.
Solution
letlanguage="JavaScript";console.log(language.length);
Output
10
Step-by-step Explanation
- The variable
languagestores"JavaScript". .lengthreturns the number of characters in a string."JavaScript"contains 10 characters.- Therefore, the output is
10.
The length property is useful when you need to know how long a string is.
Question 3: Convert a String to Uppercase
Problem
Create a string containing "javascript" and convert it to uppercase.
Solution
letlanguage="javascript";letresult=language.toUpperCase();console.log(result);
Output
JAVASCRIPT
Step-by-step Explanation
languagestores"javascript"..toUpperCase()converts all letters to uppercase.- The result is stored in
result. - The output becomes
"JAVASCRIPT".
Question 4: Convert a String to Lowercase
Problem
Create a string containing "HELLO WORLD" and convert it to lowercase.
Solution
letmessage="HELLO WORLD";letresult=message.toLowerCase();console.log(result);
Output
hello world
Step-by-step Explanation
- The
messagevariable stores uppercase text. .toLowerCase()converts the letters to lowercase.- The converted string is stored in
result. - The output is
"hello world".
Question 5: Remove Extra Spaces from a String
Problem
A user’s name contains unnecessary spaces before and after the text. Use a string method to remove those spaces.
Solution
letname=" Rahul ";letcleanName=name.trim();console.log(cleanName);
Output
Rahul
Step-by-step Explanation
- The
namevariable contains spaces before and after"Rahul". .trim()removes whitespace from both ends of a string.- The cleaned value is stored in
cleanName. - The output contains only
"Rahul".
trim() does not remove spaces between words.
Question 6: Extract a Part of a String
Problem
Create the string "JavaScript" and extract the word "Java" from it.
Solution
letlanguage="JavaScript";letresult=language.slice(0, 4);console.log(result);
Output
Java
Step-by-step Explanation
slice()is used to extract part of a string.slice(0, 4)starts at index0.- It stops before index
4. - JavaScript indexes start from
0. - The characters at indexes
0,1,2, and3form"Java".
The positions are:
J a v a S c r i p t0 1 2 3 4 5 6 7 8 9
Question 7: Check Whether a String Contains Specific Text
Problem
Create a string "I am learning JavaScript" and check whether it contains the word "JavaScript".
Solution
letmessage="I am learning JavaScript";letresult=message.includes("JavaScript");console.log(result);
Output
true
Step-by-step Explanation
messagecontains the complete sentence..includes()checks whether a specific text exists inside the string."JavaScript"exists in the sentence.- Therefore,
.includes()returnstrue.
If the text does not exist, it returns false.
Question 8: Find the Position of a Word
Problem
Create the string "Learn JavaScript today" and find the position where "JavaScript" starts.
Solution
letmessage="Learn JavaScript today";letposition=message.indexOf("JavaScript");console.log(position);
Output
6
Step-by-step Explanation
.indexOf()searches for a specific string.- JavaScript starts after
"Learn ". - The first character of
"JavaScript"is at index6. - Therefore, the method returns
6.
String indexes start from 0.
Question 9: Replace Text in a String
Problem
Create the string "I like Java" and replace "Java" with "JavaScript".
Solution
letmessage="I like Java";letresult=message.replace("Java", "JavaScript");console.log(result);
Output
I like JavaScript
Step-by-step Explanation
- The original string contains
"Java". .replace()searches for the specified text."Java"is replaced with"JavaScript".- The updated string is stored in
result. - The original string remains unchanged.
Question 10: Split a Sentence into Words
Problem
Create the sentence "JavaScript is easy" and convert it into an array containing each word separately.
Solution
letsentence="JavaScript is easy";letwords=sentence.split(" ");console.log(words);
Output
[ 'JavaScript', 'is', 'easy' ]
Step-by-step Explanation
sentencecontains three words..split(" ")uses the space character as the separator.- JavaScript separates the sentence wherever it finds a space.
- The result becomes an array.
- Each word becomes a separate array element.
The resulting array contains:
JavaScriptiseasy
Key Takeaways
- Strings are used to store text in JavaScript.
- Strings can be written using single quotes, double quotes, or template literals.
.lengthreturns the number of characters in a string..toUpperCase()converts text to uppercase..toLowerCase()converts text to lowercase..trim()removes whitespace from the beginning and end of a string..slice()extracts part of a string..includes()checks whether specific text exists in a string..indexOf()finds the position of text inside a string..replace()replaces matching text..split()divides a string into an array.- JavaScript string indexes start from
0. - Most string methods return a new value rather than changing the original string.
FAQs
1. What is a string in JavaScript?
A string is a sequence of characters used to represent text.
letname="Rahul";
Here, "Rahul" is a string.
2. How do I find the length of a string?
Use the .length property.
letword="Hello";console.log(word.length);
Output:
5
3. What does toUpperCase() do?
toUpperCase() converts all alphabetic characters in a string to uppercase.
console.log("hello".toUpperCase());
Output:
HELLO
4. What does toLowerCase() do?
toLowerCase() converts alphabetic characters to lowercase.
console.log("HELLO".toLowerCase());
Output:
hello
5. What is the difference between slice() and substring()?
Both methods can extract part of a string, but they handle negative indexes differently. slice() supports negative positions, while substring() treats negative values as 0.
6. How can I check whether a string contains a word?
Use the includes() method.
letmessage="I love JavaScript";console.log(message.includes("JavaScript"));
Output:
true
7. Does a string method change the original string?
Most JavaScript string methods do not change the original string. Instead, they return a new string or value.
For example:
letname="rahul";letupperName=name.toUpperCase();console.log(name);console.log(upperName);
Output:
rahulRAHUL
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
