[JavaScript] Type Conversions and Equality

In JavaScript, types are very flexible. JavaScript converts types as needed. But this flexibility of types makes developers confused in some cases. Therefore, it is important to understand the conversion rules.

 

1. Automatic Conversion

In JavaScript programming, you need to rely on automatic conversion in most of the times. The conversion rules make sense in general.

 

2. Explicit Type Conversion

The simplest way to perform the type conversion is to use the following wrapper functions:

  • Number()
  • String()
  • Boolean()
  • Object()
alert(1 + "3"); // 13
alert(1 + Number("3")); // 4
alert(String(false) + "!"); // "false!"
alert(Boolean(Infinity) ? "T" : "F"); // "T"

 

3. Converting Numbers to Strings

All numbers in JavaScript are actually “Number” objects. The “Number” object provides methods to convert numbers to strings.

The easiest way to do convert a number to a string is to use the “toString()” method. But what if you want to format the number in a specific way?

  • toFixed(x) : the number of digits after the decimal point
  • toExponential(x) :  the number of digits after the decimal point
  • toPrecision(x) : the number of digits
var n = 12.3456789;
alert(n.toFixed(2)); // 12.35
alert(n.toExponential(3)); // 1.235e+2
alert(n.toPrecision(6)); // 12.3457

Note that the value is rounded.

 

4. Converting Strings to Numbers

String values can be converted to numbers using the following functions:

  • Number.parseInt(string, radix)
  • Number.parseFloat(string)
var i = "10";
var f = "20.3";
var result = Number.parseInt(i, 10) * 2 + Number.parseFloat(f); // 40.3
alert("10 * 2 + 20.3 = " + result);

 

5. Checking Equality

The most common way to compare 2 values is to use the “==” and “!=” operator.

The “==” operator compares 2 values somewhat loosely. Even though the types are different, it still tries to compare the values by converting the type of one of the two values. Therefore (1 == “1”) returns true.

alert(1 == "1"); // true
alert(1 == true); // true
alert(0 == false); // true

 

6. Strict Equality (Identity) Operator

Unlike other languages, JavaScript provides other types equality operators : “===“, and “!==“.

If values are different types, they are not equal.

alert(1 === 1.0); // true
alert(1 === "1"); // false
alert(1 === true); // false
alert(0 === false); // false

 

7. Null and Undefined Values

You can also check null or undefined using the strict equality operator.

var nil = null;
var und;
alert(nil === null); // true
alert((typeof und) === "undefined"); // true
alert(und === undefined); // true

Checking undefined values is a little bit tricky.

var x;

You declared the variable “x”, but not “y”.

if (typeof x === "undefined") {
  // works
}
if (x === undefined) {
  // works
}
if (x) {
  // works
}
if (typeof y === "undefined") {
  // works
}
if (y === undefined) {
  // throws an exception
}
if (y) {
  // throws an exception
}

Note when the exception is thrown.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s