Search [Enter] to search. Click anywhere to close.

JavaScript Exercises for Beginners, Part 1: Strings

These JavaScript exercises are aimed specifically at beginners. So if you’re just starting out on your JavaScript journey, why not give them a go?

Alongside this article, feel free to check out the other instalments in the series.

Just like this article, the other instalments are specifically written to help you practice JavaScript via short, concise exercises.

My other articles containing beginner JavaScript exercises are:

Prerequisites – things to consider before tackling these JavaScript exercises

As a prerequisite, you should understand the basic concepts of JavaScript and have some familiarity with the toolkit and syntax at hand. The rest of the problem solving will require some fairly simple logic as well as the effective application of the native JavaScript toolkit.

It’s important to note that not all solutions are equal. Some solutions are more efficient than others. To determine how efficient your solution actually is, you’ll need some familiarity with the Big O Notation; but that’s certainly not compulsory for these exercises.

These JavaScript exercises for beginners are short, standalone exercises. You can delve in to the Big O Notation and the intricacies of algorithms later on.

As the solutions to most of these exercises make use of fairly standard operations, there are often in-built solutions for them. For instance, reversing a string. In these cases — these inbuilt solutions are disallowed. This is clearly stated within each exercise description. Coming up with your own solution is much more fun and more conducive to learning!

Now, let’s begin.

The JavaScript exercises

1. Reverse that string

This one is a classic. Simply reverse the string without using reverse().

const reverseString = (someString) => {
    // your code

console.log(reverseString("I am a string")) // gnirts a ma I

2. Is it a palindrome?

Another classic. You can apply your previous solution to help with this one.

Does the given string spell out the same word when written out backwards? For example kayak.

const isPalindrome = (someString) => {
    // your code

console.log(isPalindrome("racecar")) // true
console.log(isPalindrome("apple")) // false

3. Remove specific characters from a string

What’s the most efficient way to strip specific characters from a string? Without using replace().

Your function must strip multiple different characters from the provided string.

const removeCharacters = (someString, charactersToRemove) => {
    // your code

console.log(removeCharacters("I am an example string", ["a", "x"]))
// I m n emple string

4. Find the most common character within the string

Given any string, find the most commonly occurring character.

If there are 2 or more most commonly occurring characters; return the character which comes first alphabetically.

For instance:


Would return a, as a and b both occur 3 times each, respectively, but a comes before b alphabetically.

const mostCommonCharacter = (someString) => {
    // your code

console.log(mostCommonCharacter("abcddefg")) // d
console.log(mostCommonCharacter("heggbdeff")) // f

5. Find the repeating characters

Return an array containing all repeating characters that occur in the provided string.

Your function should accept a second parameter that defines how many times the target character should repeat before it is returned.

const findDuplicates = (someString, repeatTimes) => {
    // your code

console.log(findDuplicates("abcddef", 2)) // [d]
console.log(findDuplicates("tfghhhplffzaaa", 3)) // [h, a]

6. Is the nth letter a vowel or a consonant?

Determine whether the letter at the supplied index in the provided string is either a vowel or a consonant.

const vowelOrConsonant = (someString, position) => {
    // your code

console.log(vowelOrConsonant("hello", 4)) // consonant
console.log(vowelOrConsonant("cat", 2)) // vowel

7. Sort the string alphabetically

Return the supplied string in alphabetical order. Your function must be able to sort the string in either direction; so either ascending or descending alphabetical order.

const sortString = (someString, reverseOrder) => {
    // your code

console.log(sortString("bacd", false)) // "abcd"
console.log(sortString("axedzpq", true)) // "zxqpeda"

8. Is the string in alphabetic order?

Similarly, what’s the most efficient way to check if a string is in alphabetic order? Your function must be able to verify reverse-alphabetic order, also.

const isOrderedAlphabetically = (someString, reverseOrder) => {
    // your code

console.log(isOrderedAlphabetically("abbcddeikl", false)) // true
console.log(isOrderedAlphabetically("jheca", true)) // true

9. Capitalize the first letter of each word

Write a function to capitalize the first letter of each word in the provided string.

const capitalizeFirstLetterOfEachWord = (someString) => {
    // your code

console.log(capitalizeFirstLetterOfEachWord("i am a string")) // I Am A String

10. Search for a string within a string

Does the target string contain the provided string? No use of indexOf() or similar functions is allowed.

const stringContains = (someString, stringToSearchFor) => {
    // your code

console.log(stringContains("catapult", "cat")) // true

11. Shuffle the string

Write a function to completely randomize a given string. The string should be scrambled entirely, in a random a manner as possible.

const shuffle = (someString) => {
    // your code

console.log(shuffle("hello")) // lelho

12. Alternating caps

Return the supplied string, but every other letter should be capitalized.

const alternateCaps = (someString) => {
    // your code

console.log(alternateCaps("I am a string")) // I aM a StRiNg

13. Quote extractor

Given a quote in a format like this:

“[QUOTE]” – [author]

Return an array containing both the quote text and the author. Both components (quote, author) could also contain hyphens (-).

const extractQuote = (quote) => {    
    // your code

const quote = `"The unexamined life is not worth living." - Socrates`
// ["The unexamined life is not worth living.", "Socrates"]

14. Verify the email address

In this JavaScript exercise, write a function to determine whether an email address is valid or not.

const verifyEmail = (email) => {
    // your code

console.log(verifyEmail("somebody@somewhere")) // false
console.log(verifyEmail("")) // true

Where are the solutions to these JavaScript exercises?

There are no solutions in this article.

That’s because seeking out solutions to the exercises at hand is another key skill that aspiring web developers will need to possess. Being a good developer isn’t about knowing everything, nobody knows everything!

It’s about knowing how — how to effectively solve problems.

If you’re stuck, what pieces are you missing? You’ll need to think about that before you can begin to diagnose what you are missing with regards to the JavaScript exercise at hand.

The answers to your questions will surely reside in sites like Stack Overflow or even across various tutorials and guides on YouTube. There are no tricks required in this list of JavaScript exercises for beginners. Each exercise can be solved with common, in-built JavaScript functions when applied with the required logic.

Where to now?

If you enjoyed these JavaScript exercises for beginners or if you’re just stuck for something to build, why not check out my article on 5 interesting JavaScript project ideas?

Having a suitably challenging project to focus on can be a great way to pick up new skills and stay motivated! Motivation is the key here. If you practice JavaScript each and every day, in one form or another, you’ll no doubt begin to see improvements over time.

More JavaScript exercises for beginners

If you’re looking for more beginner JavaScript exercises like the ones above, check out the other instalments in the series. As stated, working on small, concise, isolated problems is a great way to practice JavaScript.

The other articles in this series are:

These articles are just like this one; only they target JavaScript exercises dealing specifically with arrays and timing functions respectively.

For general advice and tips on how to improve at JavaScript in general (as well as effective ways to practice), I’d recommend my other article: How Do I Get Better At JavaScript? (18 Tips)

Thanks for reading!

Have any questions? Ping me over at @justacodingblog
← Back to blog