Practice 50 JavaScript String Programming Questions, TechnoVlogs

Practice 50 JavaScript String Programming Questions


Q1. Write a JavaScript program to reverse a given string.  
Input: str = "hello"  
Expected Output: olleh

Q2. Write a JavaScript program to check if a given string is a palindrome.  
Input: str = "madam"  
Expected Output: true

Q3. Write a JavaScript program to find the length of a string.  
Input: str = "hello"  
Expected Output: 5

Q4. Write a JavaScript program to convert a string to lowercase.  
Input: str = "HELLO"  
Expected Output: hello

Q5. Write a JavaScript program to convert a string to uppercase.  
Input: str = "hello"  
Expected Output: HELLO

Q6. Write a JavaScript program to count the number of vowels in a string.  
Input: str = "hello"  
Expected Output: 2

Q7. Write a JavaScript program to find the index of the first occurrence of a character in a string.  
Input: str = "hello", char = "l"  
Expected Output: 2

Q8. Write a JavaScript program to replace all occurrences of a substring in a string.  
Input: str = "hello world", oldSub = "world", newSub = "universe"  
Expected Output: hello universe

Q9. Write a JavaScript program to extract a substring from a string.  
Input: str = "hello world", start = 0, length = 5  
Expected Output: hello

Q10. Write a JavaScript program to check if a string contains a given substring.  
Input: str = "hello world", subStr = "world"  
Expected Output: true

Q11. Write a JavaScript program to concatenate two strings.  
Input: str1 = "hello", str2 = "world"  
Expected Output: helloworld

Q12. Write a JavaScript program to remove whitespace from the beginning and end of a string.  
Input: str = "   hello   "  
Expected Output: hello

Q13. Write a JavaScript program to check if a string starts with a given substring.  
Input: str = "hello world", subStr = "hello"  
Expected Output: true

Q14. Write a JavaScript program to check if a string ends with a given substring.  
Input: str = "hello world", subStr = "world"  
Expected Output: true

Q15. Write a JavaScript program to repeat a string a given number of times.  
Input: str = "hello", times = 3  
Expected Output: hellohellohello

Q16. Write a JavaScript program to check if a string contains only digits.  
Input: str = "12345"  
Expected Output: true

Q17. Write a JavaScript program to find the last index of a character in a string.  
Input: str = "hello", char = "l"  
Expected Output: 3

Q18. Write a JavaScript program to remove a specific character from a string.  
Input: str = "hello", charToRemove = "l"  
Expected Output: heo

Q19. Write a JavaScript program to convert a string into an array of words.  
Input: str = "hello world"  
Expected Output: ["hello", "world"]

Q20. Write a JavaScript program to check if a string is empty.  
Input: str = ""  
Expected Output: true

Q21. Write a JavaScript program to find the first non-repeating character in a string.  
Input: str = "swiss"  
Expected Output: w

Q22. Write a JavaScript program to split a string into an array of characters.  
Input: str = "hello"  
Expected Output: ["h", "e", "l", "l", "o"]

Q23. Write a JavaScript program to check if a string is a valid email address.  
Input: email = "test@example.com"  
Expected Output: true

Q24. Write a JavaScript program to check if a string is a valid phone number (US format).  
Input: phone = "(123) 456-7890"  
Expected Output: true

Q25. Write a JavaScript program to capitalize the first letter of each word in a string.  
Input: str = "hello world"  
Expected Output: Hello World

Q26. Write a JavaScript program to find the number of words in a string.  
Input: str = "hello world"  
Expected Output: 2

Q27. Write a JavaScript program to find the longest word in a string.  
Input: str = "hello to the world"  
Expected Output: hello

Q28. Write a JavaScript program to find the shortest word in a string.  
Input: str = "hello to the world"  
Expected Output: to

Q29. Write a JavaScript program to remove all digits from a string.  
Input: str = "hello123world456"  
Expected Output: helloworld

Q30. Write a JavaScript program to check if a string is a valid URL.  
Input: url = "https://www.example.com"  
Expected Output: true

Q31. Write a JavaScript program to check if a string contains only alphabets.  
Input: str = "hello"  
Expected Output: true

Q32. Write a JavaScript program to find the number of occurrences of a character in a string.  
Input: str = "hello", char = "l"  
Expected Output: 2

Q33. Write a JavaScript program to remove all vowels from a string.  
Input: str = "hello"  
Expected Output: hll

Q34. Write a JavaScript program to check if a string is a valid hexadecimal color code.  
Input: color = "#FF5733"  
Expected Output: true

Q35. Write a JavaScript program to escape special characters in a string.  
Input: str = "hello (world)"  
Expected Output: hello \(world\)

Q36. Write a JavaScript program to compare two strings.  
Input: str1 = "hello", str2 = "world"  
Expected Output: false

Q37. Write a JavaScript program to trim a string to a specified length.  
Input: str = "hello world", length = 5  
Expected Output: hello

Q38. Write a JavaScript program to repeat a string until a specified length.  
Input: str = "ab", length = 7  
Expected Output: abababa

Q39. Write a JavaScript program to insert a substring into a string at a given position.  
Input: str = "hello world", subStr = "beautiful ", position = 6  
Expected Output: hello beautiful world

Q40. Write a JavaScript program to check if a string contains only whitespace characters.  
Input: str = "    "  
Expected Output: true

Q41. Write a JavaScript program to find the ASCII value of a character.  
Input: char = "A"  
Expected Output: 65

Q42. Write a JavaScript program to convert a string to an array of individual words.  
Input: str = "hello world"  
Expected Output: ["hello", "world"]

Q43. Write a JavaScript program to find if a string is a substring of another string.  
Input: str = "hello world", subStr = "world"  
Expected Output: true

Q44. Write a JavaScript program to get a string with only the first letter of each word capitalized.  
Input: str = "hello world"  
Expected Output: Hello World

Q45. Write a JavaScript program to find the position of the second occurrence of a character in a string.  
Input: str = "hello", char = "l"  
Expected Output: 3

Q46. Write a JavaScript program to convert a string to a number.  
Input: str = "12345"  
Expected Output: 12345

Q47. Write a JavaScript program to get a string with each word reversed.  
Input: str = "hello world"  
Expected Output: olleh dlrow

Q48. Write a JavaScript program to get a string without the first and last characters.  
Input: str = "hello"  
Expected Output: ell

Q49. Write a JavaScript program to check if a string contains only non-special characters.  
Input: str = "hello123"  
Expected Output: true

Q50. Write a JavaScript program to count the number of words starting with a particular letter in a string.  
Input: str = "hello world, how are you", letter = "h"  
Expected Output: 3

Share on Social Media