Documente Academic
Documente Profesional
Documente Cultură
Java Script:
• Variable ,object,functions,operator
• Forms,events
Code Editor
– Notepad++
– Atom
– Subline text
Web Browser
- Chrome
- Mozilla
Firebug
Type in subline code editor and save it with the name of index.html
<!DOCTYPE html>
<html>
<head>
</head>
<script type="text/javascript">
alert("Hello World");
</script>
<body>
</body>
</html>
Uses of Java Script:
– Sliders
– Image Gallaries
– Form Validations
Variables are used to hold or store the data during the program execution.
• String
• Number
• Boolean
• Array
• Objects
Using:
<script src="script.js"></script>
Create
alert("Hello World");
in JavaScript.
<!doctype html>
<html lang="eng">
<head>
<meta charset="utf-8">
<title>JavaScript</title>
</head>
<body>
script src="script.js"></script>
</body>
</html>
var msg="Kamran";
alert(typeof msg);
- var salary=2500;
- alert(typeof salary);
<script src="script.js"></script>
Var sum;
Alert(typeof sum)
• Result: undefind.
Null dataypes:
Var sum=null;
Alert(typeof sum);
alert(sum);
Alert(sum);
Boolean:
Alert(typeof sum);
• Sytax:
if(condition)
//execute if block
}
Else
Example of if-else
• if(5==4)
• }
• else{
• }
• Arithmetic Operators in JS
– Additions +
– Subtraction –
– Multiplication *
– Division /
– Increment ++
– decrement+--
examples of Operators
• Example:
var sum=10+50;
alert(sum);
Subtraction:
var ans=10-5;
alert(ans);
Multiplication:
Var ans=10*5;
Alert(ans);
• ==(equal to)
• !=(not equal)
• >(greater than)
• <(less Than)
var a=10,b=10;
if(a==b){
}
===(equal to- it used to check the condition as well as datatype):
Example:
var a=10,b="10";
if(a===b){
else{
var a=10,b=20;
if(a!=b){
}
(>) Greater than:
Example:
var a=10,b=8;
if(a>b){
alert("a is greater");
var a=10,b=20;
if(a<b){
alert("a is smaller");
var cAge=18;
if(cAge>=18){
Another example:
var cAge=17;
if(cAge>=18){
else{
Example:
var eage=59;
if(eage<=60){
else{
var eage=61;
if(eage<=60){
else{
-Logical OR ||:
-Logical NOT !:
Logical AND && example:
var candidateAge=25;
else
Another example:
var candidateAge=31;
else
Example:
var dressColor="blue";
if(dressColor=="black" || dressColor=="blue"){
else
Another example
var dressColor="red";
if(dressColor=="black" || dressColor=="blue"){
else
Logical NOT operator: if the condition is false than it return true and if the condition is
true than return false.’
Example:
var a=10,b=5;
alert(!(a==b));
another example
var a=10,b=10;
alert(!(a==b));
Document.write() function in Java Script:-It is use to write something in document.
document.write("Hello World");
Or
document.write("<h1>Hello World</h1>");
Or
document.write("<h1>Hello World</h1>
<p>
• var a=10,b=5;
• if(a==b){
• }
• else{
• }
Sytax:
document.write(Date());
• var result= confirm("Are you sure you want to visit this websit");
• document.write(result);
=====================================
var result= confirm("Are you sure you want to visit this websit");
if(result==true){
else{
Confirm Box
• var result= confirm("Are you sure you want to visit this websit");
• if(result==true){
• }
• else{
• }
• var result= confirm("Are you sure you want to visit this websit");
• if(result==true){
• }
• else{
• }
• var result= confirm("Are you sure you want to visit this websit");
• if(result==true){
• }
• else{
• }
Functions in Java Script:
– Function eliminates the need of writing the same code again and again.
– function alertMsg()
– {
– alert ("Hello");
– }
– alertMsg();
• function addNum(){
• var a=10,b=20;
• alert(a+b);
• }
• addNum();
• ===========================================
• function addNum(a,b){
• alert(a+b);
• }
• addNum(10,20);
• function addNum(a,b,c){
• document.write(a+b-c+"<br/>");
• }
• addNum(10,20,10);
• addNum(20,25,35);
• function addNum(a,b,c){
• return a+b-c;
• }
• alert(result1);
• function addNum(a,b,c){
• return a+b-c;
• }
• var newVal=10;
• alert(result1+newVal);
Methods: Methods are the function that let the object to do something.
• person.name="Kamran";
• person.age=35;
• alert(person.name);
• alert(person.age);
• var person={
• firstName : "Kamran",
• age:35,
• fulldata:function(){
• }
• };
• alert(person.fulldata());
example:
• var person={
• firstName : "Kamran",
• age:35,
• fulldata:function(){
• return "Person name is "+this.firstName +" and his age is" +
this.age;
• }
• };
• alert(person.fulldata());
alert(msg.length);
Array in Java Script: JavaScript arrays are used to store multiple values in a single
variable.
Example:
var student=["kamran","sana","wajahat"];
alert(student);
another example:
var student=["kamran","sana","wajahat"];
alert(student[7]);
document.write(student[8]);
var student=["kamran","sana","wajahat"];
student.push("karan");
student.unshift("jack");
document.write(student);
var student=["kamran","sana","wajahat"];
student.pop("karan");
document.write(student);
document.write(student1.concat(student2));
var student=["Rishab","kamran","json","maria"];
document.write(student);
console.log(student);
var student=["Rishab","kamran",[1,2,3,4,5],"json","maria"];
document.write(student);
console.log(student);
=============
var student=["Rishab","kamran",[1,2,3,4,5],"json","maria"];
document.write(student[2]);
console.log(student[2]);
if, if else, nested if statement:-
var a=10;
var b=10;
var c=30;
if(a==b)
else
Nested Statement:
var dresscolor="blue";
if(dresscolor=="blue")
else