The Sanskaar Valley School
Class 8 – Computer Science
Copy Work
Date: __/___/____
CHAPTER 5
JAVASCRIPT IN HTML DOCUMENT
KEYWORDS:
<script> tag, variable, operators, document.write(), parseFloat(), parseInt(), Script block, window.alert(),
window.confirm(), windowprompt(), String concatenation.
Q1. Give JavaScript statements to perform the following tasks:
a. Concatenating (joining) two strings: “Click” and “Start”.
<HTML>
<HEAD>
<BODY><
SCRIPT language = “JavaScript”>
var str1, str2
str1= “Click”
str2= “Start”
document.write(str1+str2+“ </Br>”); //to add a space in between
document.write(str1+ “ “ +str2);
</SCRIPT>
</BODY>
</HEAD>
</HTML>
b. Using single line and multiple line comments.
Single line comments:
// This is comment line 1
Multiple line comments:
/* This is comment line 1
This is comment line 2 */
c. Declaring four variables: length, breadth, height, and volume.
var length, breadth, height, volume
d. Display a confirm dialog box with the message “Plant more trees”.
<HTML>
<BODY>
<SCRIPT language=“JavaScript”>
var reply = confirm (“Plant more trees”)
document.write(reply);
</SCRIPT>
</BODY>
</HTML>
Q2. Describe the use of the following methods in JavaScript:
a. document.write(): The document.write() method is used for displaying the text on the browser
window. It uses an object called document, which refers to the current document on the browser
window.
b. window.prompt(): The prompt dialog box is also a method of the window object. This method is used
for getting input from the user. It displays a Explorer User Prompt dialog box with a message, and an
input field.
c. window.alert(): window. alert () is a method of the window object. It is the simplest dialog box used
to display a short message to the user in a separate small window. The dialog box contains the text
given as a parameter to the alert() method and a button labelled OK.
d. window.confirm(): This method returns true if OK is pressed and false if Cancel is pressed.
e. parseInt(): parseInt() method converts a string value into integers (numbers without decimal places).
f. ParseFloat(): parseFloat() method converts a string value into floating numbers (numbers with
decimal values).
Q3. What is the object model in JavaScript.
Ans: Like most other programming languages of its generation, JavaScript is also characterized by the
Object Model. This means that you think about your JavaScript program in terms of the objects you want
to work with. For programming purposes, the browser window, the HTML document, forms, etc. are the
objects which in turn are formed of other objects such as Text Boxes and Radio buttons.
Q4. Discuss various types of operators in JavaScript.
Ans: Various types of operators in JavaScript are discussed below.
a. Arithmetic operators: These operators are used to perform arithmetic calculations using variables
or constants.
b. Comparison operators: These operators are used to test if two variables relate to each other in
the specified way.
c. Logical operators: These operators perform logical operations on variables.
d. String operator: A string is a set of characters. To join two or more string values together, you use
the plus ‘+’ operator. This is also known as string concatenation operator.
✓ Do Activity given on Page no 91 and 100.
✓ Lab Work on page no 104
(paste in notebook)