Lesson - Javascript Basics
Lesson - Javascript Basics
Lesson Plan
• Core Javascript Syntax
• Types and Objects
• Functions
• DOM
• Event Handling
• Debugging
• Javascript Best Practices
What you should know
-terminated by a semi-colon
Activity 1
<html>
<head><title>Simple Page</title>
</head>
<body>
<h1>Simple HTMLPage</h1>
<p> This is very simple HTML page</p>
<p>It's about as basic as they come. It has : </p>
<ul>
<li>An h1 tag </li>
<li>Two paragraphs </li>
<li>An ordered list </li>
</ul>
<script>
alert(
"Hello World!"
);
</script>
</body>
</html>
Where to put javascript?
The statement:
Document.write(“Welcome to Nashville”);
•Block comments
/*
This line is a part of block comment.
This line is also part of block comment.
/*
Including <script> element for each
code section
<h1>Multiple Script Sections</h1>
<h2>First script section</h2>
<script type=“text/javascript”>
document.write(“ <p>Output from the first script section</p>” );
</script>
<h2>Second script section</h2>
<script type=“text/javascript”>
document.write(“ <p>Output from the second script
section</p>” );
</script>
Logic and Debugging
The console can be used for quick experimentation, like for the
upcoming concepts.
Firefox Chrome
Go to Developer->Tools->Console or
Download the Firebug extension for Firefox.
press CTRL+SHIFT+J.
Basic Variables
Use var to declare a variable. Its initial value will be
undefined.
var x;
It will work if you don't use var, but then it will become a global
variable, which you usually don't want.
undefined
name
name = “jasmine”;
jasmine
Basic Data Types
Variables are loosely typed. They can be assigned to any data
type, and can later be re-assigned to different data types.
var x;
x = "6";
x = 2 + 2;
x = false;
x = null;
Single quotes can be used inside double quotes, & vice versa:
var x = "then he said, 'thats awesome!'";
var y = 'then he said, "thats awesome!"';
** Activity 2
Strings <-> Numbers
Strings can be converted to numbers by doing simple math on
them:
var x = "43.232" * 1;
This indexOf function returns the start index of the first occurrence of a
given character or substring, or -1 if not found.
greeting.indexOf("e"); // 1
greeting.indexOf("there") // 6
greeting.indexOf("E") // -1
greeting.toUpperCase().indexOf("E") ** Activity 2
String Methods
The slice method is a handy way to create substrings.
slice(start, end) returns a copy of the string beginning at start
and extending up to but not including end:
'Hello'.slice(1, 3); // "el"
<script>
var x = "Hello World";
alert(x);
</script>
<script src="external.js"></script>
Outputting to HTML
The absolute simplest way to modify the HTML of a page using
JavaScript is with the document.write method:
<script type="text/javascript">
document.write("Hello World");
</script>
The console status bar will display "1 Error", and you can open
up the console for more information. It will often point to the line
of code that caused the error, and show a trace of the functions
called before the error was called.
+
-
/
*
Shorthand
score= score + 10; score+=10;
score= score - 10; score-=10;
score= score / 10; score/=10;
score= score * 10; score*=10;
Operator precedence
Ans= 55
symbols
( ) parentheses
[ ] brackets
{ }braces
Operators:
< !=
> ===
== >=
<=
Control Structures
Control structures
– allows us to change the ordering of how the statements in
our programs are executed
Two types of Control Structures
Types:
– if-statement
– if-else-statement
– If-else if-statement
if-statement
if( boolean_expression )
statement;
or
if( boolean_expression ){
statement1;
statement2;
}
Conditional Code
if ( condition){
if( boolean_expression ){
statement1;
statement2;
...
}
else{
statement3;
statement4;
...
}
Sample program:
else
document.write("Sorry you failed");
Sample program2:
= assignment operator
== comparing equality … if the value is the same
=== strict equality ,value and data type must be the same
Sample code:
}
Sample code:
var a =5;
var b ="5";
if(a==b){
alert("Yes they are equal");
}
else{
alert("Yes they are NOT equal");
}
if-else-else if statement:
if( boolean_expression1 )
statement1;
else if( boolean_expression2 )
statement2;
else
statement3;
Sample Program:
/*Create a script that will compute the real estate tax, given
the following formulas:
a.)If value of real estate is less than 250,001.00, tax is 5% of
real estate value.
b.) If value of real estate is between 250, 001.00 to
500,000, tax is 10% of the real estate value.
c.)If more than 500,000 tax is 15% of the real estate value.*/
Exercise
/*Create a script that will check the number and print either
ODD or EVEN*/
Exercise
if(a==b || c==d)
Exercise:
Grade:
90 – 100 = “Excellent”
80 – 89 = “Good job”
60 – 79 = “Study Harder”
59 – below = “Sorry you failed”
Modulus operator : %
var num = 3;
var mod= num% 2;
if(mod==0){
alert("Even");
}
else{
alert("Odd");
}
Unary Operator
Increment
a++;
++a;
Decrement
a--;
--a;
While Loop
var a =1;
while(a<10){
document.write("Sha");
a++;
}
Do while
var a=1;
do {
document.write();
a++;
}
while(a<10);
For Loop