[JavaScript] Types and Values

Does JavaScript have types like C#? The “var” keyword is used to declare a variable, isn’t it? The answer is “Yes”. But, JavaScript DOES have types. Just variables are not typed.

 

1. Types

JavaScript types can be divided in two categories:

  • Primitive Types
  • Object Types

JavaScript is Object-Oriented Language. Most types are objects and even inherit from the “Object” object. Primitive types include strings, numbers, and boolean values.

 

2. Text

A string is a sequence of 16-bit UNICODE characters. String literals are enclosed within a pair of single or double quotes.

You can get the length of a string using the “length” property.

var s = "Hello, World!";
alert(s.length); // 13

 

3. Numbers

  • base-10 integer values :  100, 1024 …
  • hexadecimal : 0xFF
  • floating-point values : 1.23
  • exponential values : 2.03e5 (2.03 * 10^5)

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

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

[Note] It is always a good practice to provide the radix value to the “parseInt()” function.

There are special values defined in a global scope.

  • Infinity : such divide by zero
  • NaN : Not a Number
var i = 10 / 0; // Infinity
var n = parseInt("NosuchNumber", 10); // NaN
alert(i + ", " + n);

Comparing values to Infinity or NaN requires attention. You cannot compare numbers directly with them. Use the following global functions:

  • isNaN(val) : returns true when the value is NaN
  • isFinite(val) : returns true when the value is not NaN, Infinite, or -Infinite
var i = 10 / 0; // Infinity
var n = parseInt("NosuchNumber", 10); // NaN
alert(i === Infinity); // true (possible but do not use it)
alert(n === NaN); // false!!!
alert(isFinite(i)); // false
alert(isNaN(n)); // true

In ES2015, the number-related global function are moved to the “Number” class. The global functions should not be used anymore. Please refer to my recent post: [JavaScript – ES2015] Number and Math

 

4. Boolean Values

A boolean value represents “true” and “false“.

Converting from other types to a boolean type is very flexible.

alert(0 ? "true" : "false"); // false
alert(1 ? "true" : "false"); // true
alert("" ? "true" : "false"); // false
alert("0" ? "true" : "false"); // true
alert("1" ? "true" : "false"); // true
alert(null ? "true" : "false"); // false
alert(NaN ? "true" : "false"); // false
alert(Infinity ? "true" : "false"); // true

 

5. typeof operator

To determine the type of a value, you can use the “typeof” operator. It returns the type name as a string.

var i = 1;
alert(typeof i); // "number"

var f = 2.3;
alert(typeof f); // "number"

var infinity = 10 / 0;
alert(typeof infinity); // "number"

var nan = parseInt("ab");
alert(typeof nan); // "number"

var s = "Hello World!";
alert(typeof s); // "string"

var b = true;
alert(typeof b); // "boolean"

var a = new Array();
alert(typeof a); // "object"

 

6. Undefined Values

If you check the type of a variable that is not initialized, the “undefined” is returned.

var u;
alert(typeof u); // "undefined"
alert(u === undefined); // true
alert(typeof u === "undefined"); // true

 

7. Null Values

Null values are for the object types. The variable of a null value does not refer to any object. The “typeof” operator returns “object“.

var nil = null;
alert(typeof nil); // "object"

Note that “null” is not the same as “undefined“.

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