javascript data types

No comments
                                        JavaScript Data Types

In JavaScript there are 3 Primitive datatypes, 2 composite data types, and 2 special data types.

1) Primitive Data Types:

       i) String
       ii) Number  
      iii) Boolean

2) Composite data types (reference)
 
      i) Object
      ii) Array

3) Special Data types
 
       i)Null
       ii) undefined.

1) Primitive Data Types:
     a) String:
         A string value is a chain of zero or more unicode characters (letters, digits, and punctuation                  marks). We use the string data type to represent text in Java Script. You include string
        literals in scripts by enclosing them in single or double quotation marks.
   We can use quotes inside a string, as long as they don't match the quotes surrounding the string.
   var answer= " It's alright";
   var answer= " He is called 'Sachin'";
   var answer = ' He is called "Sachin"';
  Notice the javascript does not have a type to represent the single character.

 b) Number Data Type:
        In javascript, there is no distinction between inter and floating values; a javascript number can be either (internally, javascript represents all numbers as floating-point values).

    i)Integer Values:

Integer values can be positive whole numbers, negative whole numbers, and 0. They can be represented in base 10 (decimal), base 16 (hexadecimal), and base 8 (octal). Most numbers in JavaScript are written in decimal.


You denote hexadecimal ("hex") integers by prefixing them with a leading "0x" (zero and x|X). They can contain digits 0 through 9, and letters A through F (either uppercase or lowercase) only. The letters A through F are used to represent, as single digits, 10 through 15 in base 10. That is, 0xF is equivalent to 15, and 0x10 is equivalent to 16.

You denote octal integers by prefixing them with a leading "0" (zero). They can contain digits 0 through 7 only. A number that has a leading "0" and contains the digits "8" and/or "9" is interpreted as a decimal number.

Both hexadecimal and octal numbers can be negative, but they cannot have a decimal portion, and they cannot be written in scientific (exponential) notation.


   ii)Floating-point values:

Floating-point values can be whole numbers with a decimal portion. Additionally, they can be expressed in scientific notation. That is, an uppercase or lowercase "e" is used to represent "ten to the power of". JavaScript represents numbers using the eight byte IEEE 754 floating-point standard for numerical representation. This means you can write numbers as large as 1.79769x10308, and as small as 5x10-324. A number that contains a decimal point and that has a single "0" before the decimal point is interpreted as a decimal floating-point number.

Notice that a number that begins with "0x" or "00" and contains a decimal point will generate an error. Here are some examples of JavaScript numbers.              

JavaScript contains numbers with special values. These are:
  • NaN (not a number). This is used when a mathematical operation is performed on inappropriate data, such as strings or the undefined value
  • Positive Infinity. This is used when a positive number is too large to represent in JavaScript 
  • Negative Infinity. This is used when a negative number is too large to represent in JavaScript
  • Positive and Negative 0. JavaScript differentiates between positive and negative zero.
iii) Boolean Data Type:

  The Boolean data type can only have two. They are the literals true and false. A Boolean value is a truth-value: it specifies whether the condition is true or not.

Boolean values are especially useful in control structures. The following code combines a comparison that creates a Boolean value directly with a statement that uses it. Consider the following JavaScript code sample:

if (x == 2000) {
    z = z + 1;
}
else {
    x = x + 1;
}

The if/else statement in JavaScript performs one action if a Boolean value is true (z = z + 1), and an alternate action if the Boolean value is false(x = x + 1).
You can use any expression as a comparative expression. Any expression that evaluates to 0, null, undefined, or an empty string is interpreted asfalse. An expression that evaluates to any other value is interpreted as true. For example, you could use an expression such as:

// This may not do what you expect. See below!
if (x = y + z)

Note that the above line does not check whether x is equal to y + z, since only a single equal sign (the assignment operator) is used. Instead, the code above assigns the value of y + z to the variable x, and then checks whether the result of the entire expression (the value of x) is zero. To check whether x is equal to y + z, you need to use the following code.


// This is different from the code above!
if (x == y + z)

2)  Composite data types (reference)

        Arrays
       JavaScript arrays are written with square brackets.
       Array items are separated by commas.
      The following code declares (creates) an array called cars, containing three items (car names):
      Example:
      var cars = ["Saab""Volvo""BMW"];
   Array indexes are zero-based, which means the first item is [0], second is [1], and so on.
      Objects
                JavaScript objects are written with curly braces.
               Object properties are written as name:value pairs, separated by commas.
  var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

3) Special Data Types: 
        i)Null:
                 The null data type has only one value in JavaScript: null. The null keyword cannot be used as the name of a function or variable.
A variable that contains null contains no valid Number, String, Boolean, Array, or Object. You can erase the contents of a variable (without deleting the variable) by assigning it the null value.
Notice that in JavaScript, null is not the same as 0 (as it is in C and C++). Also note that the typeof operator in JavaScript reports null values as being of type Object, not of type null. This potentially confusing behavior is for backwards compatibility.

      ii) undefined: 
                       The undefined value is returned when you use an object property that does not exist, or a variable that has been declared, but has never had a value assigned to it.
you can check to see if a variable exists by comparing it to undefined, although you can check if its type is undefined by comparing the type of the variable to the string "undefined". 
         




No comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...