How to animate number increase in JavaScript?

Wondering how to animate number increase in JavaScript? This article is indeed for you!

In this article, we will explore different techniques and approaches to animating number increases or animating number counters in JavaScript.

We will provide you with the knowledge and tools to bring your web projects to life.

Different ways to animate number increase or animating number counters in JavaScript

To make a number increase animation in JavaScript, you need to change the value of a variable and keep updating it over time.

This creates a smooth animation effect. There are different ways to do this, each with its own advantages and uses.

Let’s look at some popular techniques.

1. Use setInterval function

To animate number increase or animating number counters in JavaScript, you can use the setInterval() function. It lets you repeatedly run a function or execute a code at regular intervals.

Here’s a simple guide to animate a number increase using setInterval():

var interval = setInterval(function() {
    $('#number').text(number);
    if (number >= target) clearInterval(interval);
    number++;
}, 30);

Here’s the complete example code:

<!DOCTYPE html>
<html>
<head>
    <title>Animate Number Increase</title>
    <style type="text/css">
        body {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        #number {
            font-size: 72px;
            font-weight: bold;
            text-align: center;
            border: 1px solid black;
            padding: 20px;
        }
        #start {
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div id="number">0</div>
    <button id="start">Start</button>

    <script type="text/javascript">
        var target = 50;
        var number = 0;
        var interval;

        document.getElementById('start').addEventListener('click', function() {
            clearInterval(interval);
            number = 0;
            interval = setInterval(function() {
                document.getElementById('number').textContent = number;
                if (number >= target) clearInterval(interval);
                number++;
            }, 30);
        });
    </script>
</body>
</html>

In the provided JavaScript code, the setInterval function is utilized to increase the value of the number variable by 1 every 30 milliseconds until it reaches the desired target value of 50.

Once the page loads, the animation begins automatically and showcases each number increment. The setInterval function returns an interval ID, which can be used with the clearInterval function to halt the interval and stop the animation.

Here’s the output:

2.Use the requestAnimationFrame()

The requestAnimationFrame() is an alternative method to animate number increases in JavaScript.

Unlike setInterval(), requestAnimationFrame() aligns with the browser’s refresh rate, leading to smoother animations.

Here’s an example of how you can utilize requestAnimationFrame():

<!DOCTYPE html>
<html>
<head>
    <title>Animate Number Increase</title>
    <style type="text/css">
        body {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        #number {
            font-size: 72px;
            font-weight: bold;
            text-align: center;
            border: 1px solid black;
            padding: 20px;
        }
        #start {
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div id="number">0</div>
    <button id="start">Start</button>

    <script type="text/javascript">
        let number = 0;
        let target = 80;

        function animateNumberIncrease() {
            if (number >= target) return;
            number += 1;
            document.getElementById("number").textContent = number;
            requestAnimationFrame(animateNumberIncrease);
        }

        document.getElementById('start').addEventListener('click', function() {
            number = 0;
            animateNumberIncrease();
        });
    </script>
</body>
</html>

In the JavaScript code given, whenever the button is clicked, the number variable is incremented by 1 using the requestAnimationFrame function. This continues until the value reaches 80.

If the button is clicked again, the number variable is reset to 0, and the animation begins anew.

Here’s the output:

3. Use a JavaScript animation library

If you’re looking for a more advanced and comprehensive solution, you have the option to use a JavaScript animation library such as GSAP (GreenSock Animation Platform) or Anime.js.

These libraries offer a plethora of animation features, including the ability to animate number increases.

Here’s an example of how you can achieve this using GSAP:

<!DOCTYPE html>
<html>
<head>
    <title>Animate Number Increase with GSAP</title>
    <style type="text/css">
        body {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        #number {
            font-size: 72px;
            font-weight: bold;
            text-align: center;
            border: 1px solid black;
            padding: 20px;
        }
        #start {
            margin-top: 20px;
        }
    </style>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/gsap.min.js"></script>
</head>
<body>
    <div id="number">0</div>
    <button id="start">Start</button>

    <script type="text/javascript">
        let number = 0;

        document.getElementById('start').addEventListener('click', function() {
            gsap.to("#number", {
                duration: 2,
                innerHTML: 100,
                roundProps: "innerHTML",
                ease: "power1.out",
                onUpdate: () => {
                    number = parseInt(document.getElementById("number").innerHTML);
                },
                onUpdateParams: ["{self}"]
            });
        });

        document.getElementById('start').addEventListener('click', function() {
            document.getElementById("number").innerHTML = 0;
            gsap.to("#number", {
                duration: 2,
                innerHTML: 100,
                roundProps: "innerHTML",
                ease: "power1.out",
                onUpdate: () => {
                    number = parseInt(document.getElementById("number").innerHTML);
                },
                onUpdateParams: ["{self}"]
            });
        });
    </script>
</body>
</html>

In the example code above, when you click the button, the text inside the div element is animated using the GSAP animation library. It smoothly changes from its current value to 100 over a span of 2 seconds.

If you click the button again, the text inside the div element resets to 0, and the animation starts over. This animation displays each number incrementally, creating a visually appealing effect.

Here’s the output:

Conclusion

In conclusion, this article explores different methods and techniques to animate number increases in JavaScript.

It covers using setInterval for regular updates, requestAnimationFrame for smoother animations, and JavaScript animation libraries like GSAP.

These methods provide programmers or developers with options to create engaging number animations in their web projects.

We are hoping that this article provides you with enough information that helps you understand the JavaScript animate number increase.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment