How is JavaScript an untyped language?

JavaScript is an untyped (or dynamically typed) language because variables can hold any data type without explicit type declaration. Unlike statically typed languages like Java, C#, C++ that require type declarations like int, char, float, JavaScript uses var, let, and const to create variables of any type.

One of the key advantages of untyped languages is the flexibility to reassign different data types to the same variable during runtime.

Dynamic Type Assignment

In this example, we create a variable named "x" and assign different data types to it. At each step, we print the type of the variable using typeof.

<html>
<head>
   <title>Example - JavaScript is an untyped language</title>
</head>
<body>
   <h2>JavaScript is an untyped language</h2>
   <p>Printing the type of variable after reassigning multiple data types into it.</p>
   <p id="result"></p>
</body>
   <script>
      let x = 10;
      document.getElementById("result").innerHTML += typeof x + "<br>";
      x = "Tutorials Point";
      
      document.getElementById("result").innerHTML += typeof x + "<br>";
      x = [1, 2, 3, 4, 5, 6];
      
      document.getElementById("result").innerHTML += typeof x + "<br>";
      x = {name: "Saurabh"};
      
      document.getElementById("result").innerHTML += typeof x + "<br>";
      x = undefined;
      
      document.getElementById("result").innerHTML += typeof x + "<br>";
      x = null;
      
      document.getElementById("result").innerHTML += typeof x + "<br>";
      x = function(){};
      document.getElementById("result").innerHTML += typeof x + "<br>";
   </script>
</body>
</html>
number
string
object
object
undefined
object
function

Type Coercion

JavaScript performs automatic type conversion when operating on different data types. For example, adding a number and a string results in string concatenation.

<!DOCTYPE html>
<html>
<head>
   <title>How is JavaScript an untyped language</title>
</head>
<body>
   <h2>How is JavaScript an untyped language?</h2>
   <p>Adding two different type variables.</p>
   <p id="result"></p>
</body>
   <script>
      let x = 2; // Number
      let y = "3"; // String
      let sum = x + y;
      document.getElementById("result").innerHTML += sum + "<br>"; // 23
      document.getElementById("result").innerHTML += typeof sum; // String
   </script>
</body>
</html>
23
string

Variable Declaration Keywords

In this example, we define three variables using different declaration keywords: var, let, and const.

<html>
<body>
<p id="result"></p>
   <script>
      var age1 = 20;
      let age2 = 18;
      const age3 = 32;
      function check(age){
         if (age > 18) {
            document.getElementById("result").innerHTML += "Qualified for driving<br>";
         } else {
            document.getElementById("result").innerHTML += "Not Qualified for driving<br>";
         }
      }
      check(age1);
      check(age2);
      check(age3);
   </script>
</body>
</html>
Qualified for driving
Not Qualified for driving
Qualified for driving

String and Boolean Concatenation

This example demonstrates how JavaScript automatically converts a boolean to a string when concatenating with a string.

<html>
<head>
</head>
<body>
   <script>
      const price = "Is price good?";
      var flag = true;
      document.write(price + flag);
   </script>
</body>
</html>
Is price good?true

Conclusion

JavaScript's untyped nature provides flexibility in variable assignment and automatic type conversion. This makes JavaScript easier to write but requires careful attention to avoid unexpected type coercion behaviors.

Updated on: 2026-03-15T21:28:27+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements