Why is JavaScript considered a loosely-typed language


Since JavaScript is a loosely typed language, you are not required to correctly predict the kind of data that will be kept in a variable. Depending on the information you supply to a variable (such as this ‘ ‘ or " " to signify string values), JavaScript will automatically type it. The type of a variable, such as int, float, boolean, or String, must be declared in many other languages, including Java.

This has both good and bad effects. Even though the type system in JavaScript allows for a lot of freedom, it lacks the ability of a highly typed system to shout at you whenever you try to add an int to an object, saving you from being forced to spend hours debugging a type error.

The typing of JavaScript is ad hoc. A function does not need to accept an integer as a parameter, nor do you need to state clearly that a string is a string. JavaScript now has a lot of versatility.

In exchange for more security and trust in the code base, you have to give up a portion of the flexibility that comes with a loosely typed language.

Even though operators typically convert values to the appropriate type according to JavaScript's loosely coupled typing system, there are some situations where we must do type conversions directly.

Despite the numerous methods in JavaScript to convert data between different types, there are two that are most frequently done −

  • Values are converted to Strings
  • Values are converted to Numbers

Implicit Conversion − There are a number of JavaScript operators and functions that automatically convert a value to the appropriate type, such as the alert() function, that accepts any value and turns it to a string. However, some operators, like the "+" operator, cause issues.

Basic Example

Input

"4" + "5"

Output

"45"

here + operator stands for string concatenation in this case.

But "5" - "3" gives output 2 by using Implicit Conversion.

Example 1

Following script explains how JavaScript performs implicit type conversion.

<!DOCTYPE html>
<html>
<title>Why is JavaScript considered a loosely-typed language - TutorialsPoint</title>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="text-align:center">
   <script>
      document.write('("5" - "3") = ' + ("5" - "3") + "<br>");
      document.write('("5" - 3) = ' + ("5" - 3) + "<br>");
      document.write('("5" * "2") = ' + ("5" * "2") + "<br>");
      document.write('("5" % "2") = ' + ("5" % "2") + "<br>");
      document.write('("5" + null) = ' + ("5" + null) + "<br>");
   </script>
</body>
</html>

Values to String Conversion

A value in JavaScript can be converted to a string using the toString() or string() functions.

The Syntax of String() function is as follows −

String(value)

Example

let myNumber = 1245;
let myString = String(myNumber);

Output

The above code will give the below output −

now myNumber contains "1245"

The Syntax of toString() function is as follows −

variableName.toString(base)

Example

let myNumber = 1245;
let myString = toString(myNumber);

Output

The above code will give the below output −

now myNumber contains "1245"

Example 2

Numbers, boolean values, and dates will all be converted to strings using the code below.

<!DOCTYPE html>
<html>
<title>Why is JavaScript considered a loosely-typed language - TutorialsPoint</title>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="text-align:center">
   <script>
      
      // The variables myNumber and myDay have been
      // given the values number and date, respectively.
      let myNumber = 155;
      let myDay = new Date('2022-10-31T06:30:00');
      
      // Number to string conversion
      document.write(" String(myNumber) = " + String(myNumber) + "<br>");
      
      // number to string conversion
      document.write(" String(myNumber + 15) = " + String(myNumber + 15) + "<br>");
      document.write(" String( 20 + 20) = " + String(20 + 20) + "<br>");
      
      // from boolean value to string conversion
      document.write(" String(false) = " + String(false) + "<br>");
      
      // From Date to string conversion
      document.write(" String(myDay) = " + String(myDay) + "<br>");
   </script>
</body>
</html>

Changing Values into Numbers

A value can be transformed into a Number using JavaScript's Number() method. It has the ability to transform any numerical text and boolean value into a Number. When dealing with strings of non-numbers, it will turn them into NaN (Not a Number).

Syntax

Number(valueToConvert)

Example

let myString = "567";
let myNumber = Number(myString);

Output

The above code will give the below output −

now myNumber contain 567(Number)

Example 3

The numerical text, dates, and boolean values are all converted to numbers using the code below.

<!DOCTYPE html>
<html>
<title>Why is JavaScript considered a loosely-typed language - TutorialsPoint</title>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="text-align:center">
   <script>
      
      // Variables myNumber and myDay, respectively,
      // have been given the values number and date.
      let myNumber = "567";
      let myDay = new Date('2022-10-31T06:30:00');
      
      // string to number conversion
      document.write(" Number(myNumber) = " + Number(myNumber) + "<br>");
      
      //A boolean value is converted to a number.
      document.write(" Number(false) = " + Number(false) + "<br>");
      document.write(" Number(true) = " + Number(true) + "<br>");
      
      // Change from date to number
      document.write(" Number(myDay) = " + Number(myDay) + "<br>");
   </script>
</body>
</html>

Example 4

If the string is not a number, it is converted to NaN, while empty or white-space strings are converted to 0.

<!DOCTYPE html>
<html>
<title>Why is JavaScript considered a loosely-typed language - TutorialsPoint</title>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="text-align:center">
   <script>
      
      // Empty string assigned
      let emptyStr = "";
      
      // White space assigned
      let whiteSpc = " ";
      
      // Non-number string assigned
      let nonStr = "Welcome to Tutorialspoint!";
      
      // Printing converted values of number
      document.write(" Number(emptyStr) = " + Number(emptyStr) + "<br>");
      document.write(" Number(whiteSpc) = " + Number(whiteSpc) + "<br>");
      document.write(" Number(nonStr) = " + Number(nonStr) + "<br>");
   </script>
</body>
</html>

Updated on: 12-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements