WT PPT U2
WT PPT U2
JavaScript
JavaScript
1. All popular web browsers support JavaScript as they provide built-in execution
environments.
2. JavaScript follows the syntax and structure of the C programming language.
3. JavaScript is a weakly typed language(dynamic typed), where certain types are
implicitly cast (depending on the operation).
4. JavaScript is an object-oriented programming language that uses prototypes rather than
using classes for inheritance.
5. It is a case-sensitive language.
First JavaScript Example
<!doctype>
<html>
<head>
<title> Fisrt Java Script Example </title>
</head>
<body>
<script>
document.write("hello world");
</script>
</body>
</html>
► The script tag specifies that we are using JavaScript.
► The document. write() function displays dynamic content through JavaScript.
► 3 Places to put JavaScript code
1. Between the body tag of HTML
2. Between the head tag of HTML
3. In .js file (external JavaScript)
External JavaScript file
► We can create external JavaScript file and embed it in many HTML pages.
► It provides code re-usability because a single JavaScript file can be used in several
HTML pages.
► An external JavaScript file must be saved by a .js extension.
► It is recommended to embed all JavaScript files into a single file. It increases the speed
of the webpage.
► The length of the code reduces as only we need to specify the location of the js file.
► If two js files are dependent on one another, then a failure in one file may affect the
execution of the other dependent file.
► The web browser needs to make an additional http request to get the js code.
► A tiny to a large change in the js code may cause unexpected results in all its dependent
files.
► If it is a few lines of code, then better to implement the internal javascript code.
JavaScript Variable
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
Javascript Data Types
► JavaScript provides different data types to hold different types of values. There are two
types of data types in JavaScript.
1. Primitive data type
2. Non-primitive (reference) data type
► JavaScript primitive data types
► There are five types of primitive data types in JavaScript. They are as follows:
► String: represents a sequence of characters e.g. "hello"
► Number: represents numeric values e.g. 100
► Boolean: represents boolean value either false or true
► Undefined: represents an undefined value
► Null: represents null i.e. no value at all
► JavaScript non-primitive data types
► The non-primitive data types are as follows:
► Object: represents instance through which we can access members
► Array: represents a group of similar values
► RegExp: represents regular expression
JavaScript Operators
= Assign X= 20
+= Add and assign var a=10; a+=20; Now a = 30
<script>
var a=20;
if(a>10){
document.write("value of a is greater than 10");
}
</script>
If...else Statement
<script>
var a=20;
if(a%2==0){
document.write("a is even number");
}
else{
document.write("a is odd number");
}
</script>
If...else if statement
<script>
var a=20;
if(a==10){
document.write("a is equal to 10"); }
else if(a==15){
document.write("a is equal to 15"); }
else if(a==20){
document.write("a is equal to 20"); }
else{
document.write("a is not equal to 10, 15 or 20"); }
</script>
► Switch statement is used to execute one code from multiple expressions.
► Syntax:
switch(expression){
case value1:
code to be executed;
break;
case value2:
code to be executed;
break;
default:
code to be executed if above values are not matched;
}
Example
<script>
var grade='B';
var result;
switch(grade){
case 'A':
result="A Grade";
break;
case 'B':
result="B Grade";
break;
case 'C':
result="C Grade";
break;
default:
result="No Grade"; }
document.write(result);
</script>
JavaScript Loops
<script>
for (var i=1; i<=5; i++)
{
document.write(i + "<br/>")
}
</script>
While loop
<script>
var i=11;
while (i<=15)
{
document.write(i + "<br/>");
i++;
}
</script>
do while loop
<script>
var i=21;
do{
document.write(i + "<br/>");
i++;
}while (i<=25);
</script>
JavaScript Functions
<script>
function getcube(number){
alert(number*number*number);
}
</script>
<form>
<input type="button" value="click" onclick="getcube(4)"/>
</form>
JavaScript Function Object
► A javaScript object is an entity having state and behavior (properties and method). For
example: car, pen, bike, chair, glass, keyboard, monitor etc.
► JavaScript is an object-based language. Everything is an object in JavaScript.
► JavaScript is template-based not class-based. Here, we don’t create a class to get the
object. But, we direct create objects.
► Creating Objects in JavaScript
► There are 3 ways to create objects.
1. By object literal
2. By creating an instance of an Object directly (using new keyword)
3. By using an object constructor (using new keyword)
JavaScript Object by object literal
<script>
emp={id:102,name:"Shyam Kumar",salary:40000}
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
By creating instance of Object
<script>
var emp=new Object();
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
By using an Object constructor
<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;
}
e=new emp(103,"Vimal Jaiswal",30000);
<script>
var emp=["Sonoo","Vimal","Ratan"];
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br/>");
}
</script>
JavaScript Array directly (new keyword)
<script>
var i;
var emp = new Array();
emp[0] = "Arun";
emp[1] = "Varun";
emp[2] = "John";
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br>");
}
</script>
JavaScript array constructor (new
keyword)
<script>
var emp=new Array("Jai","Vijay","Smith");
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br>");
}
</script>
JavaScript String
► The JavaScript math object provides several constants and methods to perform
mathematical operation.
► document.write(Math.sqrt(25))
► document.write(Math.random())
► document.write(Math.pow(2,5))
► document.write(Math.floor(25.56))
► document.write(Math.ceil(25.56))
► document.write(Math.round(2.6))
► document.write(Math.abs(-25))
Browser Object Model (BOM)
► The Browser Object Model (BOM) is used to interact with the browser.
► The default object of the browser is window means you can call all the functions of the
window by specifying window or directly.
► For example: window.alert("hello");
► Window Object : The window object represents a window in browser. An object of
window is created automatically by the browser.
► Window is the object of browser, it is not the object of javascript. The javascript
objects are string, array, date etc.
Methods of window object
Method Description
alert() displays the alert box containing message with ok
button.
confirm() displays the confirm dialog box containing
message with ok and cancel button.
Method Description
getElementsByName() returns all the elements having the given name value.
getElementsByTagName() returns all the elements having the given tag name.
getElementsByClassName() returns all the elements having the given class name.
DOM event handling
► In this example, the content of the <h1> element is changed when a user
clicks on it:
<body>
<h1 onclick="this.innerHTML = 'sorry'">Click here</h1>
</body>
► In this example, a function is called from the event handler:
► <body>
<script>
function changeText(id) {
id.innerHTML = "Ooops!";
}
</script>
Mouse hover event
<body>
<div onmouseover="mOver(this)" onmouseout="mOut(this)"
style="background-color:#D94A38;width:120px;height:20px;padding:40px;">
Mouse Over Me</div>
<script>
function mOver(obj) {
obj.innerHTML = "Thank You"
}
function mOut(obj) {
obj.innerHTML = "Mouse Over Me"
}
</script>
</body>