Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you solve FizzBuzz in JavaScript?

JavaScriptInterview Questions
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 29, 2026
7 min read

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.

Table of Contents

  • The problem statement
  • The modulo operator
  • A straightforward solution
  • Why the order of checks matters
  • A version without the combined check
  • A reusable function
  • How to explain it in an interview
  • Testing your solution
  • Frequently Asked Questions
    • Why check divisibility by 15 first?
    • Can FizzBuzz be solved without if statements?
    • What does the percent sign do in JavaScript?
    • What is the time complexity?
  • Summary
  • Related Articles

The problem statement

Print one line for each number from 1 to 100:

  • If the number is divisible by 3 and by 5, print FizzBuzz.
  • Else if it is divisible by 3, print Fizz.
  • Else if it is divisible by 5, print Buzz.
  • Otherwise print the number itself.

The first lines of output are:

text

The modulo operator

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.

javascript
javascript
Loading syntax highlighting...

A straightforward solution

javascript
javascript
Loading syntax highlighting...

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.

Why the order of checks matters

The most common mistake is testing 3 before the combined case:

javascript
javascript
Loading syntax highlighting...

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.

A version without the combined check

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.

javascript
javascript
Loading syntax highlighting...

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.

A reusable function

Interviewers often prefer a function you can test, rather than loose console.log calls.

javascript
javascript
Loading syntax highlighting...

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.

How to explain it in an interview

  1. State the approach: a loop from 1 to n with the modulo operator.
  2. Mention the pitfall: the combined case must be checked first, or handled by concatenation.
  3. State the complexity: time O(n), and O(1) extra space when printing.
  4. Mention extensibility: keep the rules in a list so adding a new divisor does not change the loop.
javascript
javascript
Loading syntax highlighting...

Testing your solution

Because fizzBuzz returns an array, you can check the important cases with plain assertions in Node.js.

javascript
javascript
Loading syntax highlighting...

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.

Frequently Asked Questions

Why check divisibility by 15 first?

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.

Can FizzBuzz be solved without if statements?

Yes, with the ternary operator or string concatenation as shown above, but the if version is the clearest.

What does the percent sign do in JavaScript?

It is the remainder operator. 10 % 3 is 1.

What is the time complexity?

O(n), because the loop runs once per number and each check takes constant time.

Summary

  • FizzBuzz prints numbers 1 to 100, replacing multiples of 3, 5 and 15 with words.
  • Use % to test divisibility and check the combined case first.
  • String concatenation with output || i avoids the special case and is easy to extend.
  • Return an array from a function to make the solution testable.

Related Articles

  • How do break and continue work in JavaScript loops?
  • How do you create strings and escape characters in JavaScript?
  • Learn JavaScript: Complete Study Roadmap, Beginner to Advanced
Share

Similar posts

  • Top JavaScript Interview Questions and Answers (2026)
  • How do startsWith and endsWith work in JavaScript?
  • How do you check if a string contains a substring?
  • How do you get string length and access characters?
  • How do you create strings and escape characters in JavaScript?
  • How do break and continue work in JavaScript loops?
  • How does the while loop work in JavaScript?
  • How does the for loop work in JavaScript?