Professional Documents
Culture Documents
Lecture 10
JavaScript Core
© 2009, DCST
PGDCET/NWT Department of Computer Science & Technology
Objectives
You will learn about
• Comments
• Variables
• Arithmetic, relational and logical
operators
• Selection control structures
Comments
• Comments are added
– To explain the code and to make the code
easy-to-understand
– To prevent execution of code. This is
helpful in debugging
• Single line comments start with //
• Multi line comments are enclosed in
/* ...*/
Comments...
Example:
<script type="text/javascript">
/*
(Multi-line comments)
The code below will write a heading and a paragraph
*/
document.write("<h1>This is a heading</h1>");
</script>
Variables
• Variables are storage areas used to hold data
values
• A variable's value can change during the
execution of a script
• A variable must have a name
• You refer to a variable by its name to display or
change its value.
• Rules for JavaScript variable names:
– Variable names are case sensitive (y and Y
are two different variables)
– Variable names must begin with a letter or
the underscore character
© 2009, DCST Javascript Core 10.5
PGDCET/NWT Department of Computer Science & Technology
Declaring Variables
String Operator ( + )
The + operator can also be used to join together
(concatenate) string variables or text values
Example 1:
txt1="What a very";
txt2="nice day";
txt3=txt1+txt2;
Variable txt3 contains "What a verynice day".
Example 2:
txt1="What a very";
txt2="nice day";
txt3=txt1+" "+txt2;
Variable txt3 contains "What a very nice day"
© 2009, DCST Javascript Core 10.9
Concatenation...
When the + operator is used on a numeric and a string
operand, the number is concatenated to the string operand
and the resultant value is a string
<html>
<head><title>JavaScript</title></head>
<body><h1>example</h1>
<script language="javascript">
var x;
x=5+5;
document.write(x); document.write(" ");
x="5"+"5";
document.write(x); document.write(" ");
x=5+"5";
document.write(x); document.write(" ");
x="5"+5;
document.write(x);
</script></body></html>
PGDCET/NWT Department of Computer Science & Technology
Escape Characters
• Here are some commonly used escapes characters that can be used
with methods like alert() and write()
\n: Inserts a new line and causes the text following the '\n' to be
placed on the new line.
\t: Inserts a tab
\r: Carriage return
\b: Backspace
\f: Form feed
\': Single quote
\": Double quote
\\: Backslash
• Example
alert("JavaScript\nis\na\nclient-side\nprogramming\nlanguage");
© 2009, DCST Javascript Core 10.11
PGDCET/NWT Department of Computer Science & Technology
Relational Operators
• Relational operators are used to compare two values.
• Given that x=5, the table below explains the relational
operators
Logical Operators
• Logical operators are used to determine the logic
between relational expressions
• Given that x=6 and y=3, the table below explains the
logical operators
Conditional Statements
• In JavaScript we have the following
conditional statements:
– if statement
– if...else statement
– nested ifs
– switch statement
If Statement
• if statement executes some code only if a
specified condition is true.
• Syntax
if (condition)
{
// code to be executed if condition is true
}
• When JavaScript encounters an if statement, it
checks the condition.
• If and only if, the condition is found to be true, the
statements enclosed within the curly braces are
executed.
© 2009, DCST Javascript Core 10.15
PGDCET/NWT Department of Computer Science & Technology
Example – if
<html>
<head><title>JavaScript</title</head>
<body>
<script language="javascript"
type="text/javascript">
<!--
var response = confirm("Have you
understood the confirm box?");
if (response == false)
{
alert("Go thru the sessions again");
}
//-->
</script>
</body>
</html>
© 2009, DCST Javascript Core 10.16
PGDCET/NWT Department of Computer Science & Technology
if-else Statement
• Each condition has two paths from which we choose
one. For example, "If it's raining, I'll stay at home else
I'll go out for a stroll.“
• Syntax
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is false
}
© 2009, DCST Javascript Core 10.17
PGDCET/NWT Department of Computer Science & Technology
Example – if-else
<html>
<head> <title>JavaScript</title> </head>
<body>
<script language="javascript" type="text/javascript">
<!--
var a = 10;
if (!(a == 10))
{
alert("The magic of JavaScript");
}
else
{
alert("The beauty of JavaScript");
}
//-->
</script> </body> </html>
© 2009, DCST Javascript Core 10.18
PGDCET/NWT Department of Computer Science & Technology
switch
• Using the switch statement, we can select
one of many blocks of code to be executed.
• Syntax
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is other than case 1
and 2
}
© 2009, DCST Javascript Core 10.19
PGDCET/NWT Department of Computer Science & Technology
Example - switch
<html>
case 2:
<head>
document.write("Two");
<title>JavaScript</title>
</head> break;
<body> case 3:
<script language="javascript" document.write("Three");
type="text/javascript"> break;
<!-- default:
var n = prompt("Enter a document.write("Not 1, 2 or
number", ""); 3");
n = parseInt(n); }
switch (n)
{ //-->
case 1: </script>
document.write("One"); </body>
break;
© 2009, DCST Javascript Core 10.20
PGDCET/NWT Department of Computer Science & Technology
Important Points
• JavaScript is Case Sensitive
• A JavaScript statement is a command to the
browser
• Though it is not necessary to end JavaScript
statements with a semi-colon, it is always a
good practice to do so
• JavaScript ignores extra spaces. You can add
white spaces to your script to make it more
readable. The following lines are equivalent:
name="Regan";
name = "Regan";
Important Points...
• Text to be output can be enclosed in single quotes(') or
double quotes("). However, if the text contains double
quotes that have to be displayed, then use single
quotes to surround the text
document.write('<h1>Javascript is "NOT" Java</h1>');
Important Points...
• Break up a Code Line
– You can break up a code line within a text
string with a backslash. The example
below will be displayed properly:
document.write("Hello \
World!");
• However, you cannot break up a code line
like this:
document.write \
("Hello World!");
Summary
You have learned about
• Variables
• Operators
• Comments
• Selection structures