Friday, February 28, 2014

JavaScript Data Types

JavaScript Data Types
Primitive Types
Object Types
  • string
  • number
  • boolean
  • null
  • undefined
fundamental Object types
specialized Object types
  • Object
  • Array
  • Function
  • Date
  • RegExp
  • Error

Checking the type of a value

The typeof operator let you determine the type of a value stored in a variable.

<script type="text/javascript">

    /* primitive types */

    console.log(typeof "hey, you");     //"string", OK
    console.log(typeof 95);             //"number", OK
    console.log(typeof true);           //"boolean", OK
    console.log(typeof null);           //it yields "object"
    console.log(typeof undefined);      //"undefined", OK  
    /* object types */

    console.log(typeof {});             //"object", OK
    console.log(typeof []);             //"object", OK 
    console.log(typeof function(){});   //it yields "function" !!!

    console.log(typeof new Date());     //"object", OK
    console.log(typeof new RegExp());   //"object", OK  
    console.log(typeof new Error());    //"object", OK

Since the typeof operator returns "object" for all objects except function objects, you can only determine whether a value is an object or a primitive type. To distinguish a class of object from another, you have to use the instanceof operator the class attribute or the constructor property.

The Number Type

In JavaScript all numbers (whether integers or floating points) are represented as 64-bit floating-point values. The 64-bit floating-point IEEE 754 standard, can represent numbers as large as ±1.7976931348623157 × 10308 and as small as ±5 × 10−324.

The JavaScript number format allows you to exactly represent all integers between −9007199254740992 (or −253)) and 9007199254740992 (or 253), inclusive. If you use integer values larger than this, you lose precision in the trailing digits.

Real numbers are infinitely many, this means that, when working with real numbers in JavaScript, the representation of the number is an approximation of the actual number.
Binary floating-point representations cannot exactly represent numbers as simple as 0.1.

((.3 - .2) == .1) == false // (.3 - .2) is not equal to .1
((.2 - .1) == .1) == true  // (.2 - .1) is equal to .1

To perform important financial calculations it's good practice to manipulate monetary values as integer cents rather than fractional euros.

The String Type

A javaScript string is an immutable sequence of 16-bit values.

JavaScript uses UTF-16, a Unicode character set with 16-bit encoding. The most commonly used characters have codepoint that fits in 16 bits, other characters are encoded using two 16-bit values known as “surrogate pairs”. So, a JavaScript string of length 2 might represent a string formed by only a single Unicode character.
The JavaScript string-manipulation methods operate on 16-bit values, not on characters: they are not aware of surrogate pairs.

// The δ character has a 16-bit code point specified by the 
// four hexadecimal digits 0x03B4. The δ character has unicode value \u03B4

// Initializing a string with the value δ:
var delta = "δ";

// or, using the unicode escape sequence for delta character:
var delta = "\u03B4";

The Boolean Type

The boolean type contains two values represented by the two keywords true and false.

null and undefined

null is a language keyword that evaluates to a special value that is used to indicate the absence of a value. In practice, null is used to indicate “no value” for numbers and strings as well as objects.

undefined is another value to indicate absence. It is the value of variables that have not been initialized and the value you get when you query the value of an object property or array element that does not exist. The undefined value is also returned by functions that have no return value, and the value of function parameters for which no argument is supplied.

null VS undefined

definition language keyword predefined global variable
meaning absence of a value absence of a value
convert to boolean value false false
typeof operator returns “object” “undefined”
use a program-level expecxted absence of value

  • value assigned to numbers, strings and objects to indicate “no value
  • value passed to function to indicate “no value
a system-level unexpecxted absence of value

  • value of not initialized variables
  • value of object properties or array elements that do not exist
  • value returned by function that do not return a value
  • value of function parameters for which no argument is supplied

No comments :

Post a Comment