What is the purpose of the var keyword in JavaScript?


Like many other programming languages, JavaScript has variables. Variables can be thought of as named containers. You can place data into these containers and then refer to the data simply by naming the container. The var keyword is used to declare variables in JavaScript.

Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows.

<script>
   <!--
      var money;
      var name;
   //-->
</script>

You can also declare multiple variables with the same var keyword as follows −

<script>
   <!--
      var money, name;
   //-->
</script>

Storing a value in a variable is called variable initialization. You can do variable initialization at the time of variable creation or at a later point in time when you need that variable.

Updated on: 12-Jun-2020

493 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements