Javascript Type conversion from variable to number using Unary(+) operator

Javascript Type conversion from variable to number using Unary(+) operator

In Javascript there are 2 types of Type Conversions

  • Implicit conversions (also called as Type Coercion in Javascript)
  • Explicit conversions (there are various ways of doing it)

In this topic we will talk about Explicit Conversion of converting a variable to a Number type using Unary(+) operator with valid content.

Below is an example showing how to do type conversion of any valid variable to Number type using Unary(+) operator.

var x = "20";
var y = +x;

The typeof operator for variable x returns String, whereas the type of a variable y returns Number.

!! Very easy and convenient !!

Leave a Reply

Your email address will not be published. Required fields are marked *