JAVASCRIPT DATA TYPES


DATA TYPE:

The types of values a program can work with are known as its data types.
The data types in JavaScript are either:
  • Primitive (the five types)
  • Non-primitive (objects,functions,arrays)

 PRIMITIVE:

JavaScript, has  following primitive data types:
  1. Number—this includes floating point numbers as well as integers, for example 1, 100, 3.14.
  2. String—any number of characters enclosed within single or double quotes, for example "a", "one", "one 2 three".
  3. Boolean—can be either true or false.
  4. Undefined—when you try to access a variable that doesn't exist, you get the special value undefined. The same will happen when you have declared a variable, but not given it a value yet. JavaScript will initialize it behind the scenes, with the value undefined.
  5. Null—this is another special data type that can have only one value, the null value. It means no value, an empty value, nothing. The difference with undefined is that if a variable has a value null, it is still defined, it only happens that its value is nothing.

    NON-PRIMITIVE:

    OBJECT:

    Any value that doesn't belong to one of the five primitive types listed above is an object.

    An object is a collection of named values, called the properties of that object. Functions associated with an object are referred to as the methods of that object.
    Properties and methods of objects are referred to with a dot.notation that starts with the name of the object and ends with the name of the property.
    Example:
    image.src.
    Normally in objects there are only two nodes, the object and the property, but sometimes the properties can have properties of their own..
    Example:
    document.form1.namefield.

    FUNCTION:

    A function is a piece of code, predefined or written by the person creating the JavaScript, that is executed based on a call to it by name.
    Any JavaScript that is not inside a function is executed the moment the Web browser reaches it when first parsing the document. Functions allow execution to be delayed. They also allow for the same piece of code to be re-used many times in the same document, since functions allow the section of code they contain to be referred to by name.
    A function is a data type in JavaScript. This is different from many other programming languages. This means that they can be treated as containing values that can be changed.

    ARRAYS:

    An Array is an ordered collection of data values.
    In JavaScript, an array is just an object that has an index to refer to its contents.The fields in the array are numbered, and you can refer to the number position of the field.
    The array index is included in square brackets immediately after the array name. In JavaScript, the array index starts with zero, so the first element in an array would be arrayName[0], and the third would be arrayName[2].

    NUMERIC KEYWORD LITERALS:

JavaScript also has some numeric keyword literals .
1.Infinity
Infinity has the value of infinity, which is to say a number larger than the largest number that JavaScript can represent.
There is also the keyword literal -Infinity for the negative infinity.

2.NaN
NaN is the value returned when you try to treat something that is not a number as a number.

ESCAPE SEQUENCE CHARACTER:

An escape sequence is a character or numeric value representing a character that is preceded by a backslash ( \ ) to indicate that it is a special character.
Some escaped characters are as follows:
Escape Sequence Character
\0 (blackslash zero)
\b Backspace.
\t Tab.
\n New line . Inserts a line break at the point specified. It is a combination of the carriage return (\r) and the form feed (\f).
\" Double quote.
\' Single quote, or an apostrophe, such as in can\'t.
\\ The backslash, since by itself it is a command.
\r Carriage return




TYPE OF OPERATOR: 

Finding out the Value Type —the typeof Operator
If we want to know the data type of a variable or a value, we can use the special typeof operator. This operator returns a string that represents the data type. The return values of using typeof can be one of the following—"number", "string", "boolean", "undefined", "object", or "function".
Example:
var n=1;
typeof n -----returns “number”




1 comment: