Which data is stored in var and how its content is changed in JavaScript?

JavaScript variables declared with var are dynamically typed, meaning they can store any type of data and their type can change during execution. The variable itself doesn't have a fixed type, but the value it holds does.

Dynamic Typing in JavaScript

Unlike statically typed languages, JavaScript allows the same variable to hold different data types throughout its lifetime. This flexibility is both powerful and requires careful handling.

Example: Variable Type Changes

<!DOCTYPE html>
<html>
  <body>
    <script>
      var a;
      document.write("Initial: " + typeof a + "<br>");
      
      a = true;
      document.write("Boolean: " + typeof a + "<br>");
      
      a = 5;
      document.write("Number: " + typeof a + "<br>");
      
      a = "web";
      document.write("String: " + typeof a + "<br>");
      
      a = [1, 2, 3];
      document.write("Array: " + typeof a + "<br>");
      
      a = {name: "JavaScript"};
      document.write("Object: " + typeof a + "<br>");
    </script>
  </body>
</html>
Initial: undefined
Boolean: boolean
Number: number
String: string
Array: object
Object: object

Data Types Stored in var

JavaScript variables can store these primitive types and objects:

<!DOCTYPE html>
<html>
  <body>
    <script>
      var data;
      
      // Primitive types
      data = undefined;        // undefined
      data = null;            // object (JavaScript quirk)
      data = true;            // boolean
      data = 42;              // number
      data = "Hello";         // string
      data = Symbol("id");    // symbol
      data = 123n;            // bigint
      
      // Reference types
      data = {};              // object
      data = [];              // object (array)
      data = function() {};   // function
      
      document.write("Variable can hold any type!");
    </script>
  </body>
</html>
Variable can hold any type!

How Content Changes

When you reassign a var variable, the old value is replaced entirely. JavaScript doesn't convert types automatically during assignment:

<!DOCTYPE html>
<html>
  <body>
    <script>
      var value = 10;
      document.write("Original: " + value + " (type: " + typeof value + ")<br>");
      
      value = "Now I'm text";
      document.write("Changed: " + value + " (type: " + typeof value + ")<br>");
      
      value = null;
      document.write("Final: " + value + " (type: " + typeof value + ")<br>");
    </script>
  </body>
</html>
Original: 10 (type: number)
Changed: Now I'm text (type: string)
Final: null (type: object)

Key Points

  • Variables declared with var can store any JavaScript data type
  • The variable's type is determined by its current value, not its declaration
  • Type changes happen when you assign a new value of a different type
  • Use typeof operator to check the current type of a variable
  • Arrays and objects both return "object" when checked with typeof

Conclusion

JavaScript's dynamic typing allows var variables to store any data type and change types freely during execution. This flexibility makes JavaScript versatile but requires careful type checking when needed.

Updated on: 2026-03-15T21:31:09+05:30

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements