50 JavaScript Practice Problems for Beginners
Here are 50 basic problems you solve with JavaScript syntax alone, with no web page involved. For each topic we picked an even spread, easiest first.
1. Input and output #1–5
Read all the input at once with require("fs").readFileSync(0, "utf8").
What you get is a string, so turn it into an array of numbers with .trim().split(/\s+/).map(Number).
Print with console.log.
For many lines, collect them in an array and print once with join("\n"), which is faster.
2. Variables and types #6–10
Use const by default and let only for values that change."3" + 4 is not an error.
It gives "34", so always convert input with Number before you calculate.
Numbers are not split into integers and decimals, and they are exact only up to about 9 quadrillion (2^53).
3. Arithmetic #11–15
7 / 2 is 3.5.
When you need the quotient, use Math.floor or Math.trunc (the two give different results for negative numbers).-7 % 2 is -1.
To print a fixed number of decimal places, use x.toFixed(2).
4. Conditionals #16–20
Compare with === and !==.== converts types as it compares, so 0 == "" is true.
For a short choice, write condition ? a : b.
5. Loops #21–25
To repeat a set number of times, use for (let i = 0; i < n; i++).
To look at each value in an array, use for (const x of arr).for...in gives you the indexes (as strings), not the values, so it is better not to use it on arrays.
6. Strings #26–30
You cannot change a string, so you have to build a new one.
Reverse with [...s].reverse().join(""), cut with slice, and pad to a width with padStart.
To put a value inside a string, use backticks, as in `Hello, ${name}`.
7. Arrays #31–35
Without a comparison function, arr.sort() sorts the values as strings, so [10, 9, 1] becomes [1, 10, 9].
Sort numbers with arr.sort((a, b) => a - b).sort and reverse change the original array, so if you still need it, copy it first with [...arr].
8. Sum, average, maximum #36–40
Get the sum with arr.reduce((a, b) => a + b, 0) and the maximum with Math.max(...arr).
If you leave out the starting value 0 in reduce, an empty array causes an error.
9. Functions #41–45
Make one with function, or write an arrow function like const f = (x) => x * 2.
If you use curly braces in an arrow function, you have to write return yourself.
Leave it out and you get undefined back.
10. Objects and Map #46–50
To look up a value by name, use an object {} or a Map.
The basic pattern for counting is count[k] = (count[k] ?? 0) + 1.
When the keys are numbers or the insertion order matters, Map is the safe choice.
Good to know
- Your output has to match down to the last character.
console.log(a, b)prints one space between the two values. - Converting something that is not a number with
NumbergivesNaN. If you seeNaNin your result, check the place where you split the input first.
See also
- 100 Python Practice Problems for Beginners — 100 problems
- 100 Coding Challenges for Beginners — 100 problems
- Learn JavaScript — lessons with explanations and examples
- Other problem sets