JavaScript Tutorial

Javascript Type Conversion

In JavaScript, you can determine the type of a value using the typeof operator and perform type conversions using various built-in functions and operators.

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 operand

Result

object

"object"

number

"number"

string

"string"

boolean

"boolean"

function

"function"

undefined

"undefined"

The following example helps in evaluating the type of an operand.

Input:-

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

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

Output:-

 

Go back to Previous Course