Type Conversion and Coercion

JavaScript Concepts Explained In-Depth

Nitin Gutte
TheLeanProgrammer

--

In this article, we are going to learn the little confusing concepts for beginners that will help you in your developer journey.

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.

Type Conversion or Explicit Conversion:

The conversion takes place manually or explicitly by typing the method or function name before converting the values, it is known as Explicit Conversion.

In JavaScript, there are five different types of data types as String, number, boolean, object, and function.

The type conversion only used in three data types which are String, number, and boolean.

— To String Conversion :

In this conversion, the number is converted to a string when we need it.

JavaScript provides different types of built-in methods or functions used in the Explicit conversion.

1.String()

The String() method is a Global method, and it is used to convert numbers to strings.

It can also be used to convert literals and expressions.

Syntax :

String(ValueToConvert)

Example:

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.

2. toString()

It does the same job as String() method.

Syntax:

number.toString()

Example:

First, we declare and assign the value to the variable then using the variable name we call the toString() method using the dot(.) operator.

If we try to pass null and undefined values, then the JavaScript engine shows TypeError.

3. toExponential()

This method can be used to convert the number into a string exponential symbol(e+number).

Syntax:

number.toExponential(OptinalParameter)

The parameter is optional which represents the number of digits after the decimal point.

Example:

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

5. toPrecision()

In this method the number is converted to a string, to format the number to the fixed length.

Syntax:

number.toPrecision(optianlParamater)

The parameter is optional which represents the number of digits.

Example :

— To Number Conversion :

The String to Number is converted using some built-in methods in JavaScript.

Built-in methods are as follows.

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

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.

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

In the first example, the number will be printed ‘123’, 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.

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

In the above examples, the behavior of the parseFloat() method is the same as the parseInt() method.

— To Boolean Conversion:

In boolean conversion, the values are converted to boolean values.

Syntax:

Boolean(valuesToBoolean)

Example:

If we pass the values like empty string(‘’), null, undefined, and NaN then the result will always be false.

Type Coercion or Implicit Coercion :

Type coercion is similar to type conversion, but the only key difference is the coercion performed automatically or implicitly by the JavaScript engine.

For example, In the built-in alert() method when we pass any values the JavaScript engine will automatically convert those values into a string and display the result.

In type coercion, the same data types are used such as String, number, and boolean, converted into their desired type.

— To String Coercion :

When a string is added with the number or non-string value using the plus(+) operator then the output of the expression is always a string.

Example:

In type coercion, if the first operand is a string and the second operand is non-string then the result always prints as a string.

Here the ‘+’ operator acts as a concatenation of two different operands.

— To Number Coercion:

The mathematical operations like Subtraction(-), Multiplication(*), Division(/), and Modulus(%) performed with the string then the output of the expression is converted into a number implicitly.

In number coercion, the plus(+) operator is not used.

Example :

If any one of the string is non-numeric or both the strings are non-numeric then the JavaScript display the result as NaN i.e., not a number.

— To Boolean Coercion:

In boolean coercion, the boolean values such as true and false are converted to a number.

In the above examples, JavaScript considers the true value as 1 and the false value as 0.

Conclusion:

The conversion and coercion terms are quite confusing at first, but you have understood everything in this article. In the interview process, the interviewer may ask questions related to these types, and you will be able to answer them without any problem.

If this article helped you please clap, comment, share, and don't forget to follow me.

Thank You For Reading!

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer

--

--

Nitin Gutte
TheLeanProgrammer

Cybersecurity and Blockchain Enthusiast. Learning to code by teaching others in a simple way. Completed my Diploma in Information Technology.