FizzBuzz asks you to print the numbers from 1 to 100, but print Fizz for multiples of 3, Buzz for multiples of 5 and FizzBuzz for multiples of both. It is a very common warm-up interview question, and the solution in JavaScript uses a loop, the modulo operator and if / else if. The key is to check the combined case (divisible by 15) first.
This guide builds the answer step by step and shows a few cleaner variants. The phrase "fizzbuzz javascript" is how most people search for it.
Print one line for each number from 1 to 100:
FizzBuzz.Fizz.Buzz.The first lines of output are:
The tool you need is the remainder operator %. a % b gives the remainder of dividing a by b. A number is divisible by b when the remainder is 0.
The loop runs 100 times. Each pass tests the conditions in order, and only the first matching branch runs. Writing i % 15 === 0 works because a number divisible by both 3 and 5 is divisible by 15. You can also write i % 3 === 0 && i % 5 === 0, which reads more directly from the problem statement.
The most common mistake is testing 3 before the combined case:
For 15, the first condition is already true, so it prints Fizz and the FizzBuzz branch can never run. Put the most specific condition first.
You can build the word by concatenation. This avoids the special case and makes it easy to add rules later, such as Bazz for 7.
For 15 both if statements run, so the output is FizzBuzz. For 7, output stays an empty string, which is falsy, so output || i prints the number.
Interviewers often prefer a function you can test, rather than loose console.log calls.
The function returns an array, so you can check it with fizzBuzz(15).at(-1) === "FizzBuzz". Returning data instead of printing is easier to test and to reuse.
Because fizzBuzz returns an array, you can check the important cases with plain assertions in Node.js.
Array positions are zero-based, so number 15 is at index 14. Testing 3, 5, 15 and a plain number covers all four branches of the logic.
A multiple of 15 is also a multiple of 3 and 5. If you test 3 or 5 first, that branch wins and the FizzBuzz output never appears.
Yes, with the ternary operator or string concatenation as shown above, but the if version is the clearest.
It is the remainder operator. 10 % 3 is 1.
O(n), because the loop runs once per number and each check takes constant time.
% to test divisibility and check the combined case first.output || i avoids the special case and is easy to extend.