JavaScript String Programs for Practice (Without Solutions)

1. Get the First and Last 2 Characters

Description: Write a JavaScript program to create a new string using the first 2 and last 2 characters of a given string. If the string length is less than 2, return an empty string.

Input: "JavaScript"

Expected Output: "Japt"


2. Find the Length of the Longest String in an Array

Description: Write a JavaScript program to find the length of the longest string from an array of strings.

Input:

["Java", "Programming", "Code"]

Expected Output:

11

3. Repeat the Last Two Characters Four Times

Description: Write a JavaScript program to create a string made of four copies of the last two characters.

Input: "Coding"

Expected Output:

ngngngng

4. Reverse a String if Its Length is a Multiple of 4

Description: Reverse the string only if its length is divisible by 4.

Input: "Code"

Expected Output:

edoC

5. Count Occurrences of a Substring

Description: Count how many times a substring appears inside a string.

Input:

String: "banana"
Substring: "an"

Expected Output:

2

6. Check Whether a Character is a Vowel or Consonant

Description: Determine whether a given alphabet is a vowel or consonant.

Input: "e"

Expected Output:

Vowel

7. Find the Longest and Shortest Word

Description: Find the longest and shortest words in a sentence.

Input:

"I love learning JavaScript"

Expected Output:

Longest: JavaScript
Shortest: I

8. Find the Most Frequently Repeated Character

Description: Find the character with the highest frequency.

Input: "programming"

Expected Output:

g

9. Calculate String Length Without Using length Property

Description: Find the length of a string using loop logic only.

Input: "JavaScript"

Expected Output:

10

10. Replace Every Second Occurrence of a Character with ‘$’

Description: Replace every second occurrence of each repeated character with the $ symbol.

Input: "Programming"

Expected Output:

Prog$am$in$

11. Swap the First and Last Character

Description: Exchange the first and last characters of the string.

Input: "JavaScript"

Expected Output:

tavaScripJ

12. Swap the First and Last Character of Every Word

Description: Exchange the first and last character of each word in a sentence.

Input:

"Online Learning"

Expected Output:

enliOn gearninL

13. Count Vowels in Each Word

Description: Count vowels present in every word and display the result as an object.

Input:

"We are learning JavaScript"

Expected Output:

{
We:1,
are:2,
learning:3,
JavaScript:3
}

14. Repeat Vowels Three Times and Consonants Twice

Description: Repeat every vowel three times and every consonant twice.

Input: "Code"

Expected Output:

CCooodee

15. Check Whether a String is a Palindrome

Description: Check whether the given string reads the same forwards and backwards.

Input: "madam"

Expected Output:

Palindrome

16. Reverse the Entire String

Description: Reverse all characters of the string.

Input: "JavaScript"

Expected Output:

tpircSavaJ

17. Calculate String Length

Description: Find the total number of characters in the string.

Input: "Programming"

Expected Output:

11

18. Count Frequency of Every Character

Description: Count the occurrences of each character.

Input: "hello"

Expected Output:

{
h:1,
e:1,
l:2,
o:1
}

19. Combine Two Strings

Description: Join two strings together.

Input:

"Hello"
"World"

Expected Output:

HelloWorld

20. Print Characters at Even Positions

Description: Display characters at even index positions.

Input: "JavaScript"

Expected Output:

JvSrp

21. Check Whether the String Contains Numbers

Description: Determine whether the string contains any numeric digit.

Input: "Code123"

Expected Output:

Contains Number

22. Count Total Vowels

Description: Count all vowels present in the string.

Input: "I love JavaScript"

Expected Output:

6

23. Count Total Consonants

Description: Count all consonants present in the string.

Input: "JavaScript"

Expected Output:

7

24. Print Characters at Odd Positions

Description: Display characters at odd index positions.

Input: "abcdefg"

Expected Output:

bdf

25. Remove Duplicate Characters

Description: Remove duplicate characters while preserving the first occurrence.

Input: "programming"

Expected Output:

progamin

26. Check Whether a String Contains Special Characters

Description: Write a JavaScript program to determine whether a given string contains any special characters.

Input:

"JavaScript@2025!"

Expected Output:

Given string contains special characters.

27. Exchange the First and Last Character of the Entire String

Description: Write a JavaScript program to swap the first and last character of the complete string.

Input:

"We are learning JavaScript"

Expected Output:

"ee are learning JavaScripW"

28. Convert All Characters to Uppercase

Description: Write a JavaScript program to convert all lowercase letters of a string into uppercase.

Input:

"I live in Pune"

Expected Output:

"I LIVE IN PUNE"

29. Remove Newline Characters

Description: Write a JavaScript program to remove newline (\n) characters from a string.

Input:

"Object Oriented Programming\n"

Expected Output:

"Object Oriented Programming"

30. Split and Join a String

Description: Write a JavaScript program to split a string into words and join them using a hyphen (-).

Input:

"Hello World"

Expected Output:

["Hello", "World"]

"Hello-World"

31. Format a Floating-Point Number

Description: Write a JavaScript program to display a floating-point number with exactly 3 decimal places and convert it into a string.

Input:

2.14652

Expected Output:

"2.147"

32. Convert Number Words into Digits

Description: Write a JavaScript program to convert number words into their corresponding numeric value.

Input:

"five four three two one"

Expected Output:

54321

33. Find the Position of a Word

Description: Write a JavaScript program to find the position of a specified word in a sentence.

Input:

Sentence: "I am solving string problems"
Word: "problems"

Expected Output:

4

34. Count Occurrences of a Word

Description: Write a JavaScript program to count how many times a word appears in a sentence.

Input:

Sentence: "We are learning JavaScript and we are practicing daily"
Word: "are"

Expected Output:

2

35. Find the Least Frequent Character

Description: Write a JavaScript program to identify the character with the lowest frequency in a string.

Input:

"abcdabdggfhf"

Expected Output:

c

36. Find Words Greater Than a Given Length

Description: Write a JavaScript program to display all words whose length is greater than the specified value.

Input:

Length: 3
Sentence: "We are learning JavaScript"

Expected Output:

["learning", "JavaScript"]

37. Get the First Four Characters

Description: Write a JavaScript program to extract the first four characters from a string.

Input:

"JavaScript"

Expected Output:

"Java"

38. Create a String Using the First Two and Last Two Characters

Description: Write a JavaScript program to create a new string using the first two and last two characters.

Input:

"JavaScript"

Expected Output:

"Japt"

39. Print the Mirror Image of a String

Description: Write a JavaScript program to display the mirror image (reverse) of a string.

Input:

"JavaScript"

Expected Output:

"tpircSavaJ"

40. Split a String on Vowels

Description: Write a JavaScript program to split a string wherever a vowel occurs.

Input:

"qwerty"

Expected Output:

["qw", "rty"]

41. Replace Multiple Words

Description: Write a JavaScript program to replace multiple words in a sentence with new words.

Input:

Sentence: "I am learning JavaScript at SQA Tools"

Replace:
JavaScript → JS
SQA Tools → Academy

Expected Output:

"I am learning JS at Academy"

42. Replace Multiple Characters

Description: Write a JavaScript program to replace multiple characters in a string simultaneously.

Input:

String: "JavaScript"

Replace:
a → 1
t → 2
i → 3

Expected Output:

"J1v1Scr3p2"

43. Remove Empty Strings from an Array

Description: Write a JavaScript program to remove empty strings from an array of strings.

Input:

["JavaScript", "", "", "Programming"]

Expected Output:

["JavaScript", "Programming"]

44. Remove Punctuation

Description: Write a JavaScript program to remove all punctuation marks from a string.

Input:

"JavaScript: is great, for web development!"

Expected Output:

"JavaScript is great for web development"

45. Find Duplicate Characters

Description: Write a JavaScript program to display all duplicate characters in a string.

Input:

"hello world"

Expected Output:

"lo"

46. Check Whether One String is a Subset of Another

Description: Write a JavaScript program to determine whether all characters of one string exist in another string.

Input:

Main String: "iamlearningjavascript"
Subset String: "jscript"

Expected Output:

true

47. Sort Characters in a String

Description: Write a JavaScript program to sort all characters of a string in ascending order.

Input:

"xyabkmp"

Expected Output:

"abkmpxy"

48. Generate a Random Binary String

Description: Write a JavaScript program to generate a random binary string of the specified length.

Input:

8

Expected Output:

10100110

49. Check Whether a Substring Exists

Description: Write a JavaScript program to determine whether a given substring exists in a string.

Input:

String: "I live in Pune"
Substring: "live"

Expected Output:

Yes

50. Find Frequency of All Substrings

Description: Write a JavaScript program to calculate the frequency of every possible substring in a string.

Input:

"abab"

Expected Output:

{
"a":2,
"ab":2,
"aba":1,
"abab":1,
"b":2,
"ba":1,
"bab":1
}

51. Check Whether Two Strings are Rotations of Each Other

Description: Write a JavaScript program to check whether one string is a rotation of another string.

Input:

String 1: "ABCD"
String 2: "CDAB"

Expected Output:

The strings are rotations of each other.

52. Remove All Whitespaces from a String

Description: Write a JavaScript program to remove all whitespace characters from a string.

Input:

"JavaScript   Programming   Language"

Expected Output:

"JavaScriptProgrammingLanguage"

53. Capitalize the First Letter of Every Word

Description: Write a JavaScript program to convert the first letter of every word to uppercase.

Input:

"welcome to javascript programming"

Expected Output:

"Welcome To Javascript Programming"

54. Convert the First Letter of Every Word to Lowercase

Description: Write a JavaScript program to convert the first letter of every word to lowercase while keeping the remaining characters unchanged.

Input:

"Hello World JavaScript"

Expected Output:

"hello world javascript"

55. Find the ASCII Value of Each Character

Description: Write a JavaScript program to display the ASCII (Unicode) value of every character in a string.

Input:

"ABC"

Expected Output:

A : 65
B : 66
C : 67

56. Count the Number of Digits in a String

Description: Write a JavaScript program to count how many numeric digits are present in a string.

Input:

"Java123Script45"

Expected Output:

5

57. Remove All Digits from a String

Description: Write a JavaScript program to remove all numeric digits from a string.

Input:

"Java123Script45"

Expected Output:

"JavaScript"

58. Replace Spaces with Hyphens

Description: Write a JavaScript program to replace every space in a string with a hyphen (-).

Input:

"I love JavaScript"

Expected Output:

"I-love-JavaScript"

59. Count the Number of Words in a Sentence

Description: Write a JavaScript program to count the total number of words in a sentence.

Input:

"JavaScript is easy to learn"

Expected Output:

5

60. Find the Middle Character of a String

Description: Write a JavaScript program to display the middle character of a string. If the string length is even, display the two middle characters.

Input:

"Python"

Expected Output:

"th"

61. Remove the First Occurrence of a Character

Description: Write a JavaScript program to remove the first occurrence of a specified character from a string.

Input:

String: "programming"
Character: "r"

Expected Output:

"pogramming"

62. Remove the Last Occurrence of a Character

Description: Write a JavaScript program to remove the last occurrence of a specified character from a string.

Input:

String: "programming"
Character: "m"

Expected Output:

"programing"

63. Find All Positions of a Character

Description: Write a JavaScript program to display all index positions of a specified character.

Input:

String: "banana"
Character: "a"

Expected Output:

[1, 3, 5]

64. Find the First Non-Repeated Character

Description: Write a JavaScript program to find the first character that appears only once in a string.

Input:

"swiss"

Expected Output:

"w"

65. Find the First Repeated Character

Description: Write a JavaScript program to find the first character that appears more than once.

Input:

"programming"

Expected Output:

"r"

66. Reverse Every Word in a Sentence

Description: Write a JavaScript program to reverse every word individually without changing the word order.

Input:

"I love JavaScript"

Expected Output:

"I evol tpircSavaJ"

67. Reverse the Order of Words

Description: Write a JavaScript program to reverse the order of words in a sentence.

Input:

"I love JavaScript"

Expected Output:

"JavaScript love I"

68. Find the Longest Palindromic Word

Description: Write a JavaScript program to find the longest palindrome word from a sentence.

Input:

"madam level racecar apple"

Expected Output:

"racecar"

69. Count Uppercase and Lowercase Letters

Description: Write a JavaScript program to count the total number of uppercase and lowercase letters.

Input:

"JavaScript123"

Expected Output:

Uppercase: 2
Lowercase: 8

70. Check Whether a String Starts and Ends with the Same Character

Description: Write a JavaScript program to determine whether the first and last characters of a string are the same.

Input:

"level"

Expected Output:

Yes

71. Remove Consecutive Duplicate Characters

Description: Write a JavaScript program to remove consecutive duplicate characters from a string.

Input:

"aabbbccdaa"

Expected Output:

"abcda"

72. Compress a String Using Character Count

Description: Write a JavaScript program to compress a string by replacing repeated characters with their frequency.

Input:

"aaabbcccc"

Expected Output:

"a3b2c4"

73. Expand a Compressed String

Description: Write a JavaScript program to expand a compressed string into its original form.

Input:

"a3b2c4"

Expected Output:

"aaabbcccc"

74. Find the Common Characters Between Two Strings

Description: Write a JavaScript program to display all common characters present in two strings.

Input:

String 1: "javascript"
String 2: "typescript"

Expected Output:

"a, s, c, r, i, p, t"

75. Remove Common Characters from Two Strings

Description: Write a JavaScript program to remove all common characters from two given strings.

Input:

String 1: "apple"
String 2: "ample"

Expected Output:

String 1: "p"
String 2: "m"

Leave a Comment