Showing posts with label Java Script Advanced. Show all posts
Showing posts with label Java Script Advanced. Show all posts

Friday, June 29, 2012

Java Script Tutorial 25 : JavaScript RegExp Object


A regular expression (RegExp) is an object that describes a pattern of characters. When you search in a text, you can use a pattern to describe what you are searching for. A simple pattern can be one single character. A more complicated pattern can consist of more characters, and can be used for parsing, format checking, substitution and more. 
Regular expressions are used to perform powerful pattern-matching and search-and-replace functions on text. Syntax of RegExp is as follow :


var patterntxt=new RegExp(somepattern,modifiers);
or
var patt=/somepattern/modifiers;


In above code, somepattern specifies the pattern of an expression. modifiers specify if a search should be global, case-sensitive, etc.


RegExp Modifiers are used to perform case-insensitive and global searches. For example we use i modifier for case-insensitive matching and g modifier for global match. Global match means, find all matches rather than stopping after the first match. For example 



var str1 = "This is some text.";
var pattern = /some text/i;
document.write(str1.match(pattern));


The result of above case-sensitive search will be : some text 



The test() method searches a string for a specified value, and returns true or false, depending on the result. For example


var pattern=new RegExp("s");
document.write(pattern.test("The sun rises in the east"));


Result of above code will be "true", as it contains the "s" string.


The exec() method searches a string for a specified value, and returns the text of the found value. It return null,  in case no match found. For example


var pattern=new RegExp("s");
document.write(pattern.exec("The sun rises in the east."));


Result of above code will be "s", as it contains the "s" string.

Java Script Tutorial 24 : JavaScript Math Object

The Math object allows you to perform mathematical tasks. The Math object includes several mathematical constants and methods. Syntax for using properties/methods of Math is as follows:


var x=Math.PI;
var y=Math.sqrt(16);


There is eight mathematical constants in JavaScript that can be accessed from the Math object. These mathematical constants are : 


E
PI
square root of 2
square root of 1/2
natural log of 2
natural log of 10
base-2 log of E
base-10 log of E.


We can call/reference these constants from JavaScript as below:


Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E


There are many mathematical methods also available in JavaScript. For example Math.round() is used to round a number, e.g  


document.write(Math.round(9.8));


Result of above code will be : 10


Math.random() method of the Math object is used to generate random numbers. For example


document.write(Math.random());


Result of above code can be anything between 0 and 1.


We can set the limit of random number generation( by using arguments ). For example, the following code return random number between 0 and 20.


Math.random()*21)


To generate random number from 0 and 100 change 21 to 101.

Thursday, June 28, 2012

Java Script Tutorial 23 : JavaScript Boolean Object


The JavaScript Boolean object represents two values, "true" or/and "false". We can create a Boolean object in javascript as follows :


var myBooleanObj=new Boolean();


Javascript boolean object will set to false, if we do not set its initial value or has one of the following values:


0
-0
null
""
false
undefined
NaN


It will be true for any other value.

Java Script Tutorial 22 : JavaScript Array Object


An array is a special variable, which can hold more than one value, at a time. For example, if you have a list of items, storing the country names in single variables could look like as follow :


var country1="Pakistan";
var country2="USA";
var country3="Russia";


An array can hold all your variable values under a single name. And you can access the values by referring to the array name. Each element in the array has its own ID so that it can be easily accessed. We can defined array in following ways :


var country=new Array(); 
country[0]="Pakistan";
country[1]="USA";
country[2]="Russia";


or


var country=new Array("Pakistan","USA","Russia");


or


var country=["Pakistan","USA"," Russia"];


You can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0. For example


document.write(country[1]);


Result of above code will be : USA


To modify a value in an existing array, just add a new value to the array with a specified index number:


country[1]="Germany";


This code change the value USA to Germany inside array object.


document.write(country[0]);


Now It will result as : Germany


Java Script Tutorial 21 : JavaScript Date Object


The JavaScript Date object is used to work with dates and times. Date objects are created with the Date() constructor. We can initiate date by four ways:

  • new Date()
  • new Date(milliseconds)
  • new Date(dateString)
  • new Date(year, month, day, hours, minutes, seconds, milliseconds)

We can operate a number of methods after creating a date object. These method allow you to get and set value of year, month, day, hour, minute, second, and milliseconds of the object. All dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds.
For example


var today = new Date()
var d1 = new Date("October 13, 1975 11:13:00")
var d2 = new Date(79,5,24)
var d3 = new Date(79,5,24,11,33,0)


Set/Manipulate Dates :
We can easily manipulate the date by using the methods available for the Date object. For example


var myDateobj=new Date();
myDateobj.setFullYear(2011,0,15);


Adding days to a date shifts the month or year, the changes are handled automatically by the Date object itself.


Compare Two Dates :
We can also use Date object to compare two dates. For example


var y=new Date();
y.setFullYear(2110,0,16);
var current = new Date();
if  ( y > current )
   {
       alert("Current Date is before 16th January 2110");
   }
 else
   {
       alert("Current Date is after 14th January 2100");
   }

Java Script Tutorial 20 : JavaScript Objects


JavaScript is an Object Based Programming language, and allows you to define your own objects and make your own variable types. 
An object is just a special kind of data. An object has properties and methods. We can create our own objects in java script, which will be discussed in future tutorials. 


Properties :
Properties are the values associated with an object. For example


<script type="text/javascript">
     var txt="This is text.";
     document.write(txt.length);
</script>


In above code, txt is an object and length is its property. Result displayed in browser will be : 13


Methods :
Methods are the actions that can be performed on objects. For example


<script type="text/javascript">
     var txt="This is Text";
     document.write(txt.toUpperCase());
</script>


In above code txt is an object and toUpperCase() is method.  Result displayed in browser will be : 
THIS IS TEXT