Unit3-Basics of JavaScript
Unit3-Basics of JavaScript
Unit3-Basics of JavaScript
Prepared by,
Divya
Asst. Prof. ISE dept.
VCET, Puttur
Overview of Javascript,
Object orientation & Javascript,
Syntactic characteristics,
Primitives, operations, &expressions,
Screen output and keyboard input,
Control statements,
Object creation and modification,
Arrays,
Functions,
Constructors,
Pattern matching using regular expressions,
Errors in scripts,
Examples.
1. OVERVIEW OF
JAVASCRIPT
Origins
The official name of the standard language is ECMAScript.
JavaScript can be divided into three parts:
the core, client side, and server side.
The core is the heart of the language, including its operators,
expressions, statements, and subprograms.
Client-side JavaScript is a collection of objects that support
the control of a browser and interactions with users.
Server-side JavaScript is a collection of objects that make the
language useful on a Web server.
OVERVIEW OF
JAVASCRIPT
OVERVIEW OF
JAVASCRIPT
Uses Of Javascript
JavaScript is implemented at 2 ends:
Client end - is embedded in XHTML documents
Server end
JavaScript support DOM [Document Object Model] which enables
JavaScript to access and modify CSS properties and content of any
element of a displayed XHTML document
OVERVIEW OF
JAVASCRIPT
OVERVIEW OF
JAVASCRIPT
JAVASCRIPT OBJECTS
Data properties :
primitive values and references to other objects
JavaScript uses non-object types for some of its simplest types; these nonobject types : primitives.
Strings, Numbers, Booleans, null, undefined
The name : values pairs (in JavaScript objects) are called properties.
Comments: // or /* .*/
primitive types
JavaScript has five primitive types: Number, String, Boolean, Undefined, and
Null.
-------------------------------------------------------- 3.
Null :
Undefined
var p=null;
var p1;
alert(p) //null
alert(p1) //undefined
Boolean
4. DECLARING VARIABLES
<html>
<head><script language="JavaScript">
<!--//
function sq()
{ var a=prompt( "Enter first number " , "" );
a=parseInt(a);
var b=prompt( "Enter first number " , "" );
b=parseInt(b);
var c=prompt( "Enter first number " , "" );
b=parseInt(b);
var max=Math.max(a,b,c);
alert("largest of three input number:"+max);
} //-->
</script></head>
<body><form><input type="button" value="sq" onclick="sq();"/>
</form>
</body></html>
<html><head>
<script language="JavaScript">
<!--//
var val = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
document.write ("Value of Number.MAX_VALUE : " + val +"<br />");
document.write ("Value of smallestNum : " + smallestNum+"<br />" );
document.write ("Value of infiniteNum : " + infiniteNum +"<br />");
document.write ("Value of negInfiniteNum : " + negInfiniteNum +"<br />");
var dayOfMonth=231;
if (dayOfMonth < 1 || dayOfMonth > 31)
{
dayOfMonth = Number.NaN
alert("Day of Month must be between 1 and 31.")
}
//-->
</script></head><body></body></html>
. a
A table of numbers from 5-15 and their squares , cubes using alert
6. CONTROL STATEMENTS :
1. Control Expressions :
Relational operators
2. Selection Statements
Write
a javacript code:
To find reverse of a given number.
..\web_exercises
\chapter4_js\4.14reverse.html
Use if and if-else statement to check
3. switch Statements
<html><head><script language="JavaScript">
<!--//
var bs=prompt(" select border size");
switch(bs) {
case "1": document.write("<table>"); break;
case "2": document.write("<table border='1'>"); break;
Case "3": document.write("<table border='8'>"); break;
default: document.write("invalid choice");break;
}
document.write("<caption>table1</caption>");
document.write("<tr><th>A</th><th>B</th>");
document.write("<tr><td>a</td><td>b</td></table>");
//-->
</script></head><body></body></html>
4. LOOP STATEMENTS
while
for and do-while
1. Using document.write(), write code that displays the
results of the 12 times table. Its output should be the
results of the calculations.
..\..\web_lab\js\12_tables.html
2. To print factorial of a number
..\..\web_lab\js\factorial.html
8. ARRAYS
1.
a.
b.
length property:
my_list_2.length = 1002;
3. Array Methods
Join Method
\chapter4_js\4.11_array_switch.html
FUNCTIONS
Return statement: returns control to the function caller..
One or more return statements in function body
function fun()
{
Alert(hi)
}
PARAMETERS
..\..\Web
The
return a - b; });
10 Median of an array:
<html><head><script language="JavaScript"> <!--//
function median(list) {
list.sort(function (a, b) {return a - b;});
Math.floor(1.6);
//1
var list_len = list.length;
//6 or 7
Math.round(1.6)
//2
if ((list_len % 2) == 1)
//0 or 1
return list[Math.floor(list_len / 2)];
else
return Math.round((list[list_len / 2 - 1] + list[list_len / 2]) / 2); //even
}
var my_list_1 = [8, 3, 9, 1, 4, 7];
var my_list_2 = [10, -2, 0, 5, 3, 1, 7];
var med = median(my_list_1);
document.write("Median of [", my_list_1, "] is: ", med, "<br />");
med = median(my_list_2);
document.write("Median of [", my_list_2, "] is: ,
med, "<br />");
//--></script></head><body></body></html>
..\web_exercises
\chapter4_js\4.8_remove_zero
s.html
1.
. The
11.
var
Above
11.
With JavaScript, you can define and create your own objects.
create and initialize the properties of newly created objects.
12
2 approaches:
RegExp
String object
/^pearl/
/gold$/
Pattern Modifiers
The i modifier makes the letters in the pattern match either uppercase
or lowercase letters in the string.
/Apple/i
matches APPLE, apple, APPle,
/^\w+/
[\.-]?
([\.-]?\w+)*
13
1.
2.
3.
14 ERRORS IN SCRIPTS
ERRORS IN SCRIPTS
..\web_exercises\chapter4_js\4.12_stringpattern.html