Lec7 JS
Lec7 JS
1. JavaScript:
HTML is used to add text formatting and linking information on web pages. HTML is not a
programming language, rather it is a presentational language. We use HTML to control the
presentation of contents on a web page. JavaScript was designed to provide programming
capability to HTML designers. JavaScript is a popular client-side scripting language i.e it runs on
the clients machine. JavaScript is an interpreted language, means that scripts execute without
preliminary compilation. Most of the browsers can understand and execute
JavaScript.JavaScript is usually embedded directly into HTML pages. Everyone can use
JavaScript without purchasing a license.
JavaScript is the most popular scripting language on the internet, and works in all major
browsers, such as Internet Explorer, Mozilla, Firefox, Netscape, Opera. JavaScript is used in
millions of Web pages to improve the design, validate forms, detect browsers, create cookies,
and much more.
Common uses of JavaScript:
JavaScript gives HTML designers a programming tool
JavaScript can react to events
JavaScript can read and write HTML elements
JavaScript can be used to validate data
JavaScript can be used to detect the visitor's browser
JavaScript can be used to create cookies
External Script: We can also write JavaScript code in a separate file. This file can be included in
the HTML document. We can use a text editor like note pad to write JavaScript code. We save a
JavaScript document with .js extension. This document is included in the HTML document using
the script tag. The src attribute of the <script> tag is used to identify the source of the
JavaScript document.
External scripts are useful when we have lengthy JavaScript code. It improves the readability of
the code. Similarly, by external script we can reuse the code as well.
In the following example we declare a JavaScript document. In this code we write a welcome
message on the browser screen. This document is included in the HTML document and displays
the message.
myscript.js Html document
document.write (“Welcome to the JavaScript”); <html>
<head>
<title> External Script </title>
<script src="myscript.js">
</script>
</head>
<body>
</body></html>
The output of the above coed is given below
JavaScript conventions: Following are some of the conventions used while writing JavaScript
code
Use of the Semicolon: JavaScript provides us the flexibility in the use of semicolon to end the
instructions. If we write single instruction we can end it with a semicolon. Similarly, we can also
end an instruction without a semicolon. However when we write multiple instructions in one
line, we have to separate them by semicolon.
Case Sensitivity: JavaScript instructions and variable names are case sensitive
Comments:we can add single line comments by // and multiple line comments by
/*
comments
*/
Use of quotes: In JavaScript single and double quotation marks can be used interchangeably
but the logical sequence is must to maintain.
3. Variables in JavaScript:
Variable is the name of a memory location which holds the data of a certain type (data types).
There are four common data types in JavaScriptnumbers, strings, Boolean and null values.
JavaScript is a loosely typed language. It means that we are not required to declare the type of
a variable. The type of the value we assign to a variable becomes the type of the variable. The
keyword “var” is used to declare a variable. Following are the examples of variable declaration
in JavaScript
– var LastName = “Smith”
– var AccountNumber = 1111
Variable Naming in JavaScript: following are the basic rules for variable naming in JavaScript
First character cannot be a digit
Other characters may be digits, letters or underscore
Reserved words cannot be used
Case sensitive
4. Operators in JavaScript:
An operator is simply a symbol that tells the compiler (or interpreter) to perform a certain
action. JavaScript provides the following types of operators.
Assignment Operator:This operator is used to assign value to a variable. JavaScript use ‘=’
symbol to assign values to variables.
Arithmetic Operators:These operators are used to perform basic arithmetic operations.
JavaScript uses + operator for addition operation, - for subtraction operation, * for
multiplication operation, / for division operation, % to find remainder, ++ to increment the
value of a variable by 1 and -- is used as decrement operator
Logical Operators:these operators are used for logical comparison. The and operation is
performed by using &&, the logical or operation is performed by || and the logical not
operation can be performed by ‘!’ symbol.
Comparison Operators: These operators are used to compare the value of two variables. The
== operator is used to check the equality of two variables while === operator can also be used
to check the equality. == operator compares only the value of two variables while === operator
compares both the value and types of variables. , The ’!=’ operator is used to check inequality, !
== operator is used to check strict inequality.
< is the less than operator, > is the greater than operator, <= is the less or equal operator and
>= is the greater or equal operator in JavaScript.
5. Input/output in JavaScript:
JavaScript provides write() function to write output. We can write output on the browser as
document.write(output)
When we have to write a message or text on the browser we use quotation marks while the
value of a variable can be written without quotation marks. The write() function can also be
used to write HTLM tags. We write HTML tag in quotation marks. + operator can be used to
concatenate strings. For example we can concatenate a message and the value of a variable
using + operator
<script>
var a=10
document.write(“<b>the value of the variable is</b>” + a)
</script>
We can use the prompt dialog box to take input from a user. When we use a prompt box, it
displays a dialog box to the user. User can enter its input in this box. User can return this input
to the script by clicking the ok button or can return null by clicking the cancel button on the
prompt box. Following code shows the use of prompt box
<script>
var n = prompt(“Please enter a number”, 0)
</script>
In the following example, we use the input and output instruction.
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<script language="javascript">
var num=prompt("Pleae Enter a Number",0)
document.write("You Entered "+num)
</script>
</body>
</html>
In the above example, we have used the prompt box and asked the user to enter a number.
User’s entered number is assigned to the variable num. Then we have used the write function
to write the input on the browser screen. Following figure shows the prompt box asking the
user to enter a number
When user enters the value, that value is displayed on the screen