How is JavaScript an untyped language?


JavaScript is an untyped language because in JavaScript the variables can hold any data type meaning that JavaScript does not have a type declaration and when the variable is created we do not need to specify any data type unlike other programming languages like Java, C#, C++, etc. which needs int, char, float, etc. to create a variable.

In JavaScript we use var, let, and const to create a variable. One of the best parts of untyped language is it gives the flexibility to reassign any type of value to a variable, it doesn't matter whether the initialized value was not the same type.

Example 1

In this example we are creating a variable named “x” and assigned a number to it and then assigned a string, and then assigned an array, and then object, undefined and null. At each step, we are printing the type of the variable “x”.

<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>

Example 2

In JavaScript, we can do any operation with two distinct types of variables.

For example, if we add 2 (type: number) with “3” (type: String) then we get “22” (type: String) instead of any error this is also a feature of untyped language.

<!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>

Example 3

In the below example, we define three variables to store three ages. We use var, let and const to define the variables.

<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>

Example 4

In the below example, we define two variables first to store a string and second to store a Boolean value. We display these two variables using the document.write().

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

Updated on: 11-Aug-2022

708 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements