Saturday, January 4, 2014

Introduction to JavaScript Series Part-2

Before going through this blog please check Part 1.

Primitive Data Types of JavaScript

Numbers

JavaScript as a single number type and it is represented internally as 64 bit floating point. Following are valid numbers in JavaScript.

var a =1; //Integer
var a=1.23; //Float
var a = 0111; //Octal, actual value is 73
var a = 0xFF; //Hexadecimal, actual value is 255

Number constructor can be used for numeric conversion.

var a = '11.11';
var b= Number(a);

Strings

In JavaScript string literal can be wrapped in single quote or double quote. It can contain any number of digits or characters. Backslash (\) is used as escape character.

var s = 'this is string';
var s = 'This is Tom\'s house'; //use of backslash to escape single //quote.
Strings can be compared with == operator and it returns true if string content and all the character cases match.

Boolean

Boolean variables can be defined true or false literals.

var a = true;

Arrays in JavaScript

Array object is used to store multiple values indexed by integer keys in a single variable.
var arr = []; //defines an empty array

var arr = [1,2,3,4]; //defines an array with four elements.

Values inside an array can be accessed using index.

var arr = [1,2,3,4];
var b= arr[0];
Here variable b will have value 1. Array constructor can also be used to declare an array.

var arr = new Array(10); //defines array with 10 elements.
length property can be used to know number of elements in an array.

var arr = [1,2,3,4,5];
var b=arr.length;

push method can be used to add an element in an array. push method will insert element at next empty index.

var arr = [];
arr.push(1);
sort method can be used to sort an array. Default sort order is alphabetic and ascending.

var arr = [];
arr.sort();
pop method can be used to remove last element from an array.

var arr = [1,2,3,4,5];
arr.pop();
reverse method can be used to reverse order of elements in an array.

var arr = [1,2,3,4,5];
arr.reverse(); //Now arr elements will be [5,4,3,2,1]
JavaScript also supports an associative array. An associative array is set of key value pairs. Values are stored in association with a key and when we provide a key, array will return its associative value.

var arr = {key1: 'value1',key2: 'value2'};
var a = arr['key1']; //variable a has value 'value1'

Functions in JavaScript

Functions contain set of reusable statements. Every function in JavaScript is instance of Native Function object of JavaScript. Function can be defined using function literal.

function add(a,b){
var c= a + b;
return c;
}
Also we can define a variable as a function.

var add = function(a,b){
var c= a + b;
return c;
}
Function can be invoked by name of the function and passing necessary arguments to it. 

add(1,2);

A function which returns value can be assigned to any variable.

var c = add(1,2); 

Objects in JavaScript

In JavaScript string, number, Boolean, null and undefined are primitive data types. All other values are objects. Class definition is not required to create objects in JavaScript. JavaScript objects are mutable keyed collection. JavaScript object contains methods and properties. Property is key value pair and methods are instance of Function object. Generally objects are used to store and organize data. Objects can contain other objects, to represent tree structure. Object literal can be specified by pair of curly braces with zero or more than one properties or methods. Objects are always passed by reference.

var obj = {}; //represent an empty object.

var obj = {
name: 'John Smith',
phone: '123456789'
}; //represent object with two properties.

Specific property value can be accessed by using name of property.

var name = obj.name; //name will have value 'John Smith'

Any attempt of accessing non existing member will return value undefined.

var email = obj.email; //email will have value undefiend.

Member can also be accessed in associative array style.

var phone = obj['phone']; //phone will have value 123456789

Any member value can be updated by assigning new value to it.

obj.name = 'Mark Smith';
obj['name'] = 'Mark Smith';

Any member of object can be deleted using delete statement.

delete obj.name;

typeof operator can be used to determine type of object property.

alert(typeof obj.name); //will alert string.

JavaScript supports inheritance by prototype model.

var base = function() {
this.baseproperty = 'Base Property';
this.overridefunction = function(){
alert('base function');
}

}
var derived = function(){
this. overridefunction = function(){
alert('derived function');
}
}

var b = new base();
derived.prototype =  b;

var d= new derived();

d.overridefunction(); // shows alert ' derived function '
var property = d. baseproperty; //stores value 'Base Property' in variable

Also a new member can be added in object as follow.

Base.prototype.myMethod = function(){
}

No comments:

Post a Comment