What Is Type Conversion in JS. List?

Timothy Joseph | February 1, 2021

What Is Type Conversion in JS. List?

In JavaScript there are 5 types of data.

To upload a file in cypress, you need to install a dependency to upload the file. Follow these steps:

  1. String
  2. Number
  3. Boolean
  4. Object
  5. Function

There are 3 most commonly used type conversions in JavaScript i.e. to string, to number, and to boolean.

Below are examples of these 3 type conversions:

For String Conversion – This is used when we output something. This is done with String(value). The conversion to string is usually obvious for primitive values. For example, alert(value) does it to show the value.

Numeric Conversion – This is used in mathematical operations. This is done with Number(value).

For example alert( "8" / "2" ); // 4, strings are converted to numbers.

For explicit conversion, Number(value) function is used to convert a value to a number as shown below:

let str = "999";
alert(typeof str); // string

let num = Number(str); // becomes a number 999
alert(typeof num); // number

We can also convert a string value like a text to number and it will fail with NaN output as shown below:

let count = Number("vakul");
alert(count); // NaN, conversion failed

Boolean Conversion

The easy of all the type conversion is Boolean one. This is used in logical operations commonly. The rules for conversion are:

  • Values which are “empty”, i.e. 0, an empty string, null, undefined, and NaN, become false.
  • Other values become true.

For example:

alert( Boolean(6) ); // true
alert( Boolean(0) ); // false
alert( Boolean("vakul") ); // true
alert( Boolean("") ); // false

Disclaimer

This publication is for informational purposes only, and nothing contained in it should be considered legal advice. We expressly disclaim any warranty or responsibility for damages arising out of this information and encourage you to consult with legal counsel regarding your specific needs. We do not undertake any duty to update previously posted materials.

Post a Comment

Categories