JavaScript Strings and String Methods Practice Questions with Solutions

Introductions

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

let message = "Welcome to JavaScript";

console.log(message);

Output

Welcome to JavaScript

Step-by-step Explanation

  1. message is a variable used to store text.
  2. "Welcome to JavaScript" is a string.
  3. The text is enclosed in double quotation marks.
  4. 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

let language = "JavaScript";

console.log(language.length);

Output

10

Step-by-step Explanation

  1. The variable language stores "JavaScript".
  2. .length returns the number of characters in a string.
  3. "JavaScript" contains 10 characters.
  4. 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

let language = "javascript";

let result = language.toUpperCase();

console.log(result);

Output

JAVASCRIPT

Step-by-step Explanation

  1. language stores "javascript".
  2. .toUpperCase() converts all letters to uppercase.
  3. The result is stored in result.
  4. The output becomes "JAVASCRIPT".

Question 4: Convert a String to Lowercase

Problem

Create a string containing "HELLO WORLD" and convert it to lowercase.

Solution

let message = "HELLO WORLD";

let result = message.toLowerCase();

console.log(result);

Output

hello world

Step-by-step Explanation

  1. The message variable stores uppercase text.
  2. .toLowerCase() converts the letters to lowercase.
  3. The converted string is stored in result.
  4. 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

let name = "   Rahul   ";

let cleanName = name.trim();

console.log(cleanName);

Output

Rahul

Step-by-step Explanation

  1. The name variable contains spaces before and after "Rahul".
  2. .trim() removes whitespace from both ends of a string.
  3. The cleaned value is stored in cleanName.
  4. 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

let language = "JavaScript";

let result = language.slice(0, 4);

console.log(result);

Output

Java

Step-by-step Explanation

  1. slice() is used to extract part of a string.
  2. slice(0, 4) starts at index 0.
  3. It stops before index 4.
  4. JavaScript indexes start from 0.
  5. The characters at indexes 0, 1, 2, and 3 form "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

let message = "I am learning JavaScript";

let result = message.includes("JavaScript");

console.log(result);

Output

true

Step-by-step Explanation

  1. message contains the complete sentence.
  2. .includes() checks whether a specific text exists inside the string.
  3. "JavaScript" exists in the sentence.
  4. Therefore, .includes() returns true.

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

let message = "Learn JavaScript today";

let position = message.indexOf("JavaScript");

console.log(position);

Output

6

Step-by-step Explanation

  1. .indexOf() searches for a specific string.
  2. JavaScript starts after "Learn ".
  3. The first character of "JavaScript" is at index 6.
  4. 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

let message = "I like Java";

let result = message.replace("Java", "JavaScript");

console.log(result);

Output

I like JavaScript

Step-by-step Explanation

  1. The original string contains "Java".
  2. .replace() searches for the specified text.
  3. "Java" is replaced with "JavaScript".
  4. The updated string is stored in result.
  5. 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

let sentence = "JavaScript is easy";

let words = sentence.split(" ");

console.log(words);

Output

[ 'JavaScript', 'is', 'easy' ]

Step-by-step Explanation

  1. sentence contains three words.
  2. .split(" ") uses the space character as the separator.
  3. JavaScript separates the sentence wherever it finds a space.
  4. The result becomes an array.
  5. 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.
  • .length returns 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.

let name = "Rahul";

Here, "Rahul" is a string.

2. How do I find the length of a string?

Use the .length property.

let word = "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.

let message = "I love JavaScript";

console.log(message.includes("JavaScript"));

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.

let message = "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:

let name = "rahul";

let upperName = name.toUpperCase();

console.log(name);
console.log(upperName);

Output:

rahulRAHUL

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top