As in our day-to-day life, we already used conversion terms. In computers, the term conversion refers to the ability to convert one file format into other file formats without any problems i.e. converting the file .JPG into .PDF or text file into an Excel file.
In programming languages, conversion is the fundamental aspect used to deal with situations facing the code.
The conversion is the way in which the variable of one type is compatible with the variable of another type to perform a particular task.
In JavaScript, you can convert a value from one type to another. This is called type conversion if it is done manually or type coercion if it done by Javascript engine . Type conversion and type coercion is one of the topics that can be hard to understand. This this article will help you with it. It will show you what it is, how it works, and how to use it to become a better JavaScript developer.
Type Conversion or Explicit conversion
The Type conversion is converting of one data types to another manually. The conversion takes place manually or explicitly by typing the method or function name before converting the values with some in-built methods. All data types as string, number, boolean and so on can be converted from one data type to another.
Converting type to string
In this conversion, Types are converted to string when they are needed
JavaScript provides different types of built-in methods or functions used in the conversion. The following methods are ways we can convert types to string in javascript.
String()
The String() method is a Global method, and it is used to convert types to strings.
It can also be used to convert literals and expressions.
Syntax :
String(ValueToConvert)
Example:
String(45) //'45'
String(2+2) // "4"
String(true) // "true"
String(false)// "false"
String(undefined) //"undefined"
String(null) //"null"
String(NaN) // "NaN"
As in the above example, the String() method converts numbers into a string as we expected.
We can pass null, NaN, and undefined as the values to the method then String() method converts them to string as well, without throwing errors.
Note:
undefined is the variable in which it is not assigned any value during its creation phase.
null is the value in which it is empty or not initialized or not defined, or it has a zero value.
NaN refers as Not-a-Number, it used to indicate an error to enter a valid number.
toString()
It does the same job as String() method.
Syntax:
number.toString()
Example:
let num = 30;
num.toString(); // "30"
First, we declare and assign the value to the variable num
then using the variable num
we call the toString() method using the dot(.) operator.
Note: If we try to pass null and undefined values, then the JavaScript engine shows TypeError.
toFixed()
The toFixed() method in JavaScript is used to convert the number into a string by specifying the number of digits to the right after the decimal point.
Syntax:
number.toFixed(optinalParameter)
The parameter is optional which represents the number of digits after the decimal point.
Example:
let num = 123.456
num.toFixed(); // 123
toPrecision()
In this method the number is converted to a string, to format the number to the fixed length.
Syntax:
number.toPrecision(Paramater)
The parameter is optional which represents the number of digits.
Example :
let num = 1234.56;
num.toPrecision(); // "1234.56"
let num2 = 1234.56;
num2.toPrecision(3); "1.23e+3
Converting types to Number
In this conversion, Types are converted to numbers when they are needed
JavaScript provides different types of built-in methods or functions used in the conversion. The following methods are ways we can convert types to Numbers in javascript.
Number()
The string value is converted to a number using the Global Number() method.
It can be used to convert numerical text as well as boolean values to numbers.
Syntax:
Number(valueToConvert)
Example:
Number('30') //30
Number('30') === 30 // true
Number('')//0
If we pass the invalid string and undefined value then the result always printed as NaN. The null value has a default value of 0. For example:
Number("Ishita") // NaN
Number(undefined) //NaN
If we pass the boolean value, then
Number(true) // 1
Number(false) // 0
parseInt()
The parseInt() method first parses or analyzes the string, and then it converts to integer i.e. number.
Syntax:
parseInt(String, Radix)
In this method the first string parameter is mandatory.
The radix is used to denote numbers to the numeral system i.e. the numbers parsed into hexadecimal to the decimal number. This parameter is optional.
Example:
parseInt("1990$this is ignored") // 1990
parseInt("Js*123") // NaN
In the first example, the number will be printed ‘1990’, and the rest of the string from ‘$’ will ignore.
In the second example, the string value must start with the number otherwise the whole string will be ignored, and the output will be printed as NaN.
parseFloat()
The parseFloat() method is used to parse or analyze the string, and then it converts to a floating-point number i.e. 3.14.
Syntax:
parseFloat(String)
The single string type parameter can only be passed in the method.
Example:
parseFloat("3.14"); // "3.14"
Converting to boolean
Boolean object converts other types values to boolean using Boolean() function. If the value is undenied, NaN, 0, ‘’"(with no space),null, it will be considered as false.
Boolean(NaN) //false
Boolean(undefined) //false
Boolean(0) //false
Boolean('') //false
Boolean(null)//false
Note: An empty string with spaces, empty object and array return true.
Boolean(' ') //true
Boolean([]) //true
Boolean({}) //true
All other values order than this gives true (conditions not included here)
Boolean('hey') //true
Boolean("false") //true
Boolean(23) //true
These are different ways of converting Javascript types manually. In my next article I will be highlighting on Javascript type coercion.