Javascript Type Conversion

We can find the type of any number, object, string and also convert that to a different data type.


Typeof Operator

The typeof operator is used to return a string that represents the type of JavaScript for a given value. It returns the data type of the operand in the form of a string. The operand can be a literal or a data structure like a function, an object, or a variable.

Syntax:-
typeof operand  
or  
typeof (operand) 

Operand is an expression that represents the object or primitive whose type is to be returned. The possible return values of the typeof operator are tabulated as follows:

Type of the operandResult
object“object”
number“number”
string“string”
boolean“boolean”
function“function”
undefined“undefined”

Following example helps in evaluate the type of an operand.

Input:-
 <script>  
	  document.write("typeof (45) is " + typeof (45) + "<br>");   
   document.write("typeof (-90) is " + typeof (-90) + "<br>");
   document.write("typeof (false) is " + typeof (false) + "<br>"); 
   document.write("typeof Null is " + typeof Null + "<br>");  
   document.write("typeof String(12) is " + typeof String(12) + "<br>");
   document.write("typeof (NaN) is " + typeof (NaN));
</script>

Output:-


Type Conversion

The process of converting one data type to another data type is called type conversion. There are two types of type conversion in JavaScript:-

  • Implicit Conversion
  • Explicit Conversion

Implicit Conversion:- In certain situations, JavaScript automatically converts one data type to another (to the right type). This is known as implicit conversion.

Input:-
 <script>  
	  let result;
   result = '3' + 2; 
   document.write("'3' + 2 is " + result + "<br>") 

   result = '4' - '2'; 
   document.write("'4' - '2' is " + result + "<br>");

   result = '3' + undefined; 
   document.write("'3' + undefined is " + result + "<br>");

   result = '3' + null; 
   document.write("'3' + null is " + result + "<br>");

   result = '4' * 2;
   document.write("'4' * 2 is " + result + "<br>");

   result = 'hello' - 'world';
   document.write("'hello' - 'world' is " + result + "<br>");

   result = 4 + false;
   document.write("4 + false is " + result + "<br>");

   result = true + undefined;
   document.write("true + undefined is " + result + "<br>");</script>

Output:-

Explicit conversion:- The process of converting one data type to another as per your needs. The type conversion that you do manually is known as explicit type conversion.

In JavaScript, explicit type conversions are done using built-in methods.

We can convert numeric strings and boolean values to numbers, we can use Number().

Input:-
 <script>  
 	  let result;
   result = Number('324');
   document.write("Number('324') is " + result + "<br>") 

   result = Number('324e-1'); 
   document.write("Number('324e-1') is " + result + "<br>");

   result = Number(false);
   document.write("Number(false) is " + result + "<br>");

   result = Number(undefined);
   document.write("Number(undefined) is " + result + "<br>");

   result = parseInt('20.01');
   document.write("parseInt('20.01') is " + result + "<br>");

   result = parseFloat('20.01');
   document.write("parseFloat('20.01') is " + result + "<br>");

   result = Math.floor('20.01');
   document.write("Math.floor('20.01') is " + result + "<br>");
</script>

Output:-