Class 9 Computer Science-Chapter 3 Introduction To Javascript
Class 9 Computer Science-Chapter 3 Introduction To Javascript
COMPUTER SCIENCE
Chapter 3
Introduction to JavaScript
Learning Objectives
• History of JavaScript
• Advantages and disadvantages of JavaScript
• Syntax of JavaScript
• Internal and external scripts
• Writing the first sample script
• Using HTML tags in script
• Escape sequences
• Comments
Introduction to JavaScript
Disadvantages
Security Issues
JavaScript snippets, once appended onto web pages execute
on client machines immediately and therefore can also be
used to exploit the user's system. Malicious code can still be
executed .
Text editor
Web browser
Javascript Syntax
•Case-sensitive
•Semicolon is command separator
Adding JavaScript in an HTML page
INTERNAL JAVASCRIPT FILE
<script>
</script>
<script type="text/JavaScript">
.
</script>
OR
<head>
<script language=“JavaScript">
alert("hello friends");
</script>
</head>
The single quotes (') and double quotes (" ") for
strings.
Sample JavaScript code
<head>
<script language=“JavaScript">
document.write(“This is my first JavaScript code");
</script>
</head>
Using HTML tags in Javascript code
<head>
<script language=“JavaScript">
document.write("<H1>"+"hello"+"</H1>");
</script>
</head>
VARIABLES
In a script (or program) we use constants and variables to store data.
The variables are the named memory locations which can be used for calculations
and temporary storage of results.
To create variables use the keyword var and value can be assigned to a variable
using = operator
Example :
var x = 5;
You can change its value just using the variable name :
x = “HELLO”;
JavaScript has no fixed types of data. A variable can hold different data
types at different times depending on context. This is called dynamic
typing feature of Javascript.
EG. y=20;
y=“APEEJAY”
Rules for naming variables
Rules for naming variables, arrays,
functions
The first character must be a letter, an underscore (_)
or $ sign.
The first character cannot be a number.
The name should not contain blank space.
D o n o t u s e re s e r v e d w o rd s t h a t a re p a r t o f t h e
JavaScript language.
Simple program to add two numbers
<html>
<head>
<script type="text/JavaScript">
var a,b;
a=parseInt(prompt("enter a number"));
b=parseInt(prompt("enter a number"));
c=a+b
document.write(c);
</script>
</head>
<body>
</body>
</html>
Simple program to print simple interest.
<html>
<head>
<script type="text/JavaScript">
var p,r,t,si;
p=parseInt(prompt("enter a principle amount"));
r=parseInt(prompt("enter a rate of interest"));
t=parseInt(prompt("enter a time taken"));
si=p*r*t/100
document.write(si);
</script>
</head>
<body>
</body>
</html>
EXTERNAL JAVASCRIPT FILE
ADVANTAGE
Reusability of code :
• we can use the same code in multiple HTML pages.
• In case of a necessary change in the JavaScript code, we just need
to modify the data in a single file (the one with the extension ".js"),
DISADVANTAGE
A".js" external file we can't use HTML tags.
EXTERNAL JAVASCRIPT FILE
<html>
<head>
<script src="code.js" type="text/JavaScript"></script>
</head>
<body>
</body>
</html>