20 tricky JavaScript string-related program output questions

Umar Farooque Khan
3 min readNov 21, 2023

--

Below is a compilation of 20 challenging JavaScript string-related programming questions, each accompanied by its solution and a brief explanation. These questions delve into various string manipulations, covering topics like concatenation, character extraction, case transformations, and more. Ideal for testing and enhancing your understanding of JavaScript’s string handling capabilities, these questions provide a comprehensive overview of common scenarios and nuances in string operations, offering valuable insights for developers looking to deepen their JavaScript expertise.

Question 1:

console.log('hello' - 'world');

Answer:

NaN

Explanation:

Subtracting strings is not a valid operation in JavaScript, resulting in NaN (Not a Number).

Question 2:

console.log('hello' + 'world');

Answer :

helloworld

Explanation :

Using the + operator concatenates strings, producing the combined string 'helloworld'.

Question 3:

console.log('hello'.length);

Answer :

5

Explanation :

The length property returns the number of characters in the string, which is 5 for 'hello'.

Question 4:

console.log('hello'.charAt(0));

Answer :

h

Explanation :

The charAt(0) method returns the character at the specified index, here 'h' at index 0.

Question 5:

console.log('hello'.toUpperCase());

Answer :

HELLO

Explanation :

toUpperCase() converts all characters in the string to uppercase, resulting in 'HELLO'.

Question 6:

console.log('hello' === 'Hello');

Answer :

false

Explanation :

Strict equality (===) compares both value and case, so 'hello' is not equal to 'Hello'.

Question 7:

console.log('hello'.split(''));

Answer :

['h', 'e', 'l', 'l', 'o']

Explanation :

split('') splits the string into an array of characters.

Question 8:

console.log('hello'.replace('l', 'L'));

Answer :

heLlo

Explanation :

replace('l', 'L') replaces the first occurrence of 'l' with 'L'.

Question 9:

console.log('hello'.indexOf('l'));

Answer :

2

Explanation :

indexOf('l') returns the index of the first occurrence of 'l' in the string.

Question 10:

console.log('hello'[1]);

Answer :

e

Explanation :

Accessing the character at index 1 using square brackets results in ‘e’.

Question 11:

console.log('hello'.concat(' ', 'world'));

Answer :

hello world

Explanation :

The concat method appends 'world' to 'hello' with a space in between.

Question 12:

console.log('hello' + 5);

Answer :

hello5

Explanation :

The + operator concatenates the string 'hello' with the number 5.

Question 13:

console.log('hello' - 5);

Answer :

NaN

Explanation :

Subtracting a number from a string results in NaN.

Question 14:

console.log('hello'.repeat(3));

Answer :

hellohellohello

Explanation :

The repeat method duplicates 'hello' three times.

Question 15:

console.log('hello'.endsWith('o'));

Answer :

true

Explanation :

Checks if ‘hello’ ends with the character ‘o’, resulting in true.

Question 16:

console.log('hello'.includes('ll'));

Answer :

true

Explanation :

The includes method checks if the string contains the specified substring ('ll' in this case).

Question 17:

console.log('hello'.slice(1, 4));

Answer :

ell

Explanation :

The slice method extracts a portion of the string (from index 1 to 3).

Question 18:

console.log('hello'.padStart(8, '_'));

Answer :

___hello

Explanation :

The padStart method pads the string with underscores to reach a length of 8 characters.

Question 19:

console.log('hello'.charAt(10));

Answer :

''

Explanation :

Accessing a character beyond the string’s length returns an empty string.

Question 20:

console.log('hello'.substring(2, 4));

Answer :

ll

Explanation :

The substring method extracts characters from index 2 to 3 (excluding the end index).

Conclusion

In conclusion, the set of 20 JavaScript string-related programming questions serves as a robust resource for developers aiming to refine their skills. Covering diverse aspects of string manipulation, the questions explore concatenation, character extraction, case transformations, and other fundamental operations. The accompanying answers and explanations offer a comprehensive understanding of intricate scenarios, fostering a deeper insight into JavaScript’s string handling capabilities. These questions not only facilitate proficiency in string-related concepts but also encourage critical thinking and problem-solving skills. Whether preparing for interviews or seeking to bolster one’s JavaScript expertise, this collection provides a valuable opportunity for developers to master the nuances of string manipulation in the JavaScript programming language.

Here are some additional high-quality tutorials for you to explore:

  1. JavaScript interview Question and Answer
  2. Node Js Interview Question and Answer
  3. JavaScript Tricky Question
  4. JavaScript Array Interview Questions

--

--

Umar Farooque Khan

Experienced software developer with a passion for clean code and problem-solving. Full-stack expertise in web development. Lifelong learner and team player.