JavaScript static variable | Create and Use examples

First, static variables preserve their previous value in their previous scope and are not initialized again in the new scope. But JavaScript doesn’t support static variables or a direct way to create them.

There is no static keywords in JavaScript. But you can implement function static variables in JavaScript and use them for purposes.

JavaScript static variable Example

Function is also a object in JavaScript and object can have its own methods and properties. You can store function variables as object’s properties.


Create JavaScript static variable

Static variable count in Function

You can change value or increase of static variables in function body. This is useful when static variable is used for counting.

Let’s see complete HTML example code for it:-


JavaScript Static variable count in Function

Do comment if you any doubts and suggestion on this JS variable topic.

Note: 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.