You are on page 1of 14

Unit-3 Javascript Basics It is a scripting language.

It was designed to add interactivity to html pages & light weight programming languages. Dynamic text into an html page. Javascript can be used to validate form data before it is submitted to a server. It is used to create cookies i.e used to store & retrieve information on the visitors computer. Client side javascript-enables the enhancement & manipulation of web pages & client browsers Server side javascript-enables back-end access to databases,file system & servers.

Difference between :
Client-side javascript It enables the enhancement & manipulation of web pages It is a javascript that runs on the client side. The tag is <script> tag The tag is <server> Server-side javascript It enables back-end access to databases,file system,servers.

Operators
1 Assignment operator-they are used to assign values. X=4,y=3 Z=x+y Z=7 Operator Example Same As x=x+y x=x-y

= += -= *= /= %=

x=y x+=y x-=y x*=y x/=y x%=y

x=x*y x=x/y x=x%y

2 Comparison Operator-are used in logical statements to determine equality or difference between the variables.
Operator == === != > < >= <= Description Is equal to Is exactly equal to Is not equal Is greater than Is less than Greater than or equal to Less than or equal to

3 Arithmetic operator-are used to perform arithmetic between variables & values. + , - , * , / , % , ++ , -4 % Modulus returns only the remainder <html> <script language=javascript> a=7%2; document.write(a=,a); </script> </html>

5 Increment Pre-increment,post-increment
<html> <head> <title>A simple page</title> <script language=javascript> var a=6; alert(++a); </script> </head> <body> </body> </html>

6 Decrement Predecrement,post-decrement <html> <head> <title>pre decrement example</title> <script type=text/javascript> var a=6; alert(--a); </script> </head> <body> </body> </html>

7 Unary operator 8 Logical operator


Operator && || ! Description and or not Example X<10 && y>1 X==5 || y==5 !(x==y)

Statements 1 for loop 2 break 3 continue 4 for in 5 comment-single line comment,multi-line comment. 6 If statement 7 if-else statement 8 if-else if else 9 switch statement

Array-It is used to store multiple values in a single variable. Object properties: length-returns the number of elements in an array. Method: Concat()-joins two or more arrays,& returns a copy of the joined array Join()-joins all elements of an array into a string Reverse()-reverses the order of the elements In an array Sort()-sorts the elements of an array.

Date-it is used to work with dates & times. date object is created with new Date() Four ways to instantiate date object Var d=new Date(); Var d=new Date(milliseconds); Var d=new date(datestring); Var d=new date(year,month,day,); Method getDate()-returns the day of the month(1-31) getDay()-returns the day of the week(0-6) getHours()-returns the hour(0-23) getMinutes()-returns the minutes(0-59) getMonth()-returns the month(0-11) getSeconds-returns the seconds(0-59) getTime-returns the no of milliseconds since midnight jan 1,1970

Math object methods abs(x)-returns the absolute value of x pow(x,y)-returns the value of x to the power of y random()-returns a random number between 0 & 1 round(x)-round x to the nearest integer. Number objects Maxvalue-returns the largest number possible in javascript Min value-returns the smallest number possible in javascript.

String String object-is used to manipulate a stored piece of text.they are created with new string() Property Length-returns the length of a string charAt()-returns the character at the specified index Concat()-joins two or more strings,& returns a copy of the joined strings Replace()-searches for a match between a substring & a string,& replaces the matched substring with a new substring. Search()-searches for a match between a regular exp & a string & returns the position of the match

Split()-splits a string into an array of substring. Substr()-extracts the characters from a string, Tolowercase()-converts a string to lowercase letters Touppercase()-converts a string to uppercase letters