JavaScript(Lec-01)
JavaScript(Lec-01)
Khalilullah Akbari
Lecturer
Department of Computer Science
Email: khalil.akbari18@gmail.com, k.akbari@kardan.edu.af
Attendance 5 Marks
What is Script?
Script is an interpreted program that runs on client computer. It is
written in scripting Language.
Scripting Language:
• JavaScript (Client Side Scripting Language)
▪ Or
▪ Dynamic (HTML+CSS+JS)
History of JavaScript
▪ JavaScript was created by Brendan Eich in 1995 in a span of 10 days. JavaScript
has become one of the world’s most popular programming language.
▪ It took Eich only 10 days to develop the scripting language, then known as
Mocha.
▪ Later, the marketing team replaced the name with 'LiveScript'. But due to
trademark reasons and certain other reasons, in December 1995, the language
was finally renamed to 'JavaScript‘ by Netscape. then, JavaScript came into
existence.
What can in-browser JavaScript do?
▪ In-browser JavaScript can do everything related to webpage manipulation,
interaction with the user, and the webserver.
▪ Add new HTML to the page, change the existing content, modify styles.
▪ React to user actions, run on mouse clicks, pointer movements, key presses.
▪ Send requests over the network to remote servers, download and upload files
▪ Get and set cookies, ask questions to the visitor, show messages.
▪ Remember the data on the client-side (“local storage”).
Application of JavaScript
▪ JavaScript is used to create interactive websites. It is mainly used
for:
▪ Client-side validation,
▪ Dynamic drop-down menus,
▪ Displaying date and time,
▪ Displaying pop-up windows and dialog boxes (like an alert dialog
box, confirm dialog box and prompt dialog box),
▪ Displaying clocks etc.
Features of JavaScript
▪ All popular web browsers support JavaScript as they provide built-in
execution environments.
▪ It is a case-sensitive language.
<script>
var x = 10;
var y = 20.2;
var z=“Ali”;
document.write(z);
</script>
Naming Conventions for JavaScript Variables
▪ A variable name must start with a letter, underscore (_).
• var arrayname=[value1,value2.....valueN];
• As you can see, values are contained inside [ ] and separated by , (comma).
• Let's see the simple example of creating and using array in JavaScript.
<script>
var emp=[“Ali",“Ahmad",“Kareem"];
document.write(emp[0] + "<br/>");
document.write(emp[1] + "<br/>");
</script>
JavaScript Array directly (new keyword)
• The syntax of creating array directly is given below:
• var arrayname=new Array();
• Here, new keyword is used to create instance of array.
• Let's see the example of creating array directly.
<script>
var emp = new Array();
emp[0] = "Saleem";
emp[1] = "Zabi";
emp[2] = "Raihan";
document.write(emp[0] + "<br>");
document.write(emp[1] + "<br>");
document.write(emp[2] + "<br>");
</script>
JavaScript array constructor (new keyword)
true
JavaScript Functions
• Function is a group of statement which is used to perform a specific task
<script>
// Sample function
function Sum()
{
var a=10;
var b=20;
document.write("a+b is equal to " + (a+b)+ "<br>" );
}
// function call
Sum();
</script>
JavaScript local and Global variables
<script>
// function with parameter
function Square( a)
{
var s=a*a;
document.write("Square of a is ="+ s+ "<br>" );
}
// function call with parameter
Square(5);
</script>
JavaScript local and Global variables
<script>
// function with return type
function Cube()
{
var b=10;
var c=b*b*b;
return c;
}
// function call with return type
// Global variable
var data=200;//gloabal variable
function a(){
document.write(data +"<br>");
}
function b(){
document.write(data +"<br>");
}
//calling JavaScript function
a();
b();
</script>
Assignment
Explain the following types of loops with practical example in JavaScript?
1.for/in
2.for/of