JavaScript increment variable by 1 | Simple Example

The increment operators in JavaScript will add one (+1) their operand, and then return a value.

Operator: x++ or ++x

JavaScript increment variable by 1 Example code

HTML example codes, increasing variable values by 1.

Postfix increment

When you use the increment operator after the operand, the increment operator increments and returns the value before incrementing.

<!DOCTYPE html>
<html>
<body>

    <script>
        let x = 3;
        x++;
        alert(x);
    </script>

</body>
</html>

Postfix increment

Use the increment operator before the operand to the increment operator increments and returns the value after incrementing.

<!DOCTYPE html>
<html>
<body>

    <script>
        let a = 3;
        ++a;
        alert(a)
    </script>

</body>
</html>
JavaScript increment variable by 1 HTML

Do comment if you have any suggestions or doubts on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.