Web Lecture # 3 (Javascript Introduction)
Web Lecture # 3 (Javascript Introduction)
Web Lecture # 3 (Javascript Introduction)
Summiya Alam
Lecture Outline
• What is JavaScript?
• Embedding JavaScript with HTML
• JavaScript conventions
• Variables in JavaScript
• JavaScript operators
• Input output in JavaScript
• JavaScript functions
• Conditional Statements
• Looping Statements
Client-side Programing
PHP already allows us to create dynamic web pages. Why also use client-side scripting? client-side
scripting (JavaScript) benefits:
usability: can modify a page without having to post back to the server (faster UI)
efficiency: can make small, quick changes to page without waiting for server
event-driven: can respond to user actions like clicks and key presses
JavaScript
• Case Sensitivity
• Comments:
– single line //
– Multiple lines /* */
JS output
• Using Quotes
• document.write(“<font color=“red”>Hello World</font>”)
• Variable Naming
– First character can not be a digit
• Variable Initialization
– var variableName = initialValue
– var variableName1 = initialValue1, variableName2 = initialValue2,
…
NULL & Undefined
• An operator is simply a symbol that tells the compiler (or interpreter) to perform a certain
action
• Assignment Operator: =
• Arithmetic Operators: +, - , *, /, %, ++,--
• Logical Operators: &&,||, !
• Comparison Operators: ==, ===, !=, !==, <, >, <=, >=
Logical Operators
• Functions are defined using the keyword function, followed by the name of the function and list
of parameters
function functionName(params)
{
..statements..
}
• Calling a function
• The syntax of a function call is:
• functionName([arguments])
Predefined functions
Common events
• onClick
• onDblClick
• onChange
• onFocus
• onMouseOver
• onMouseOut
• onSubmit
• onload
Predefined functions
• If statement
if (condition) statement
if(condition)
{ statements }
• If-else statement
if(condition)
{statement}
else
{statements}
Conditional Statements
if (condition) {
statements;
} else if (condition) {
statements;
} else {
statements;
}
JS Loops
• For loop
for(var i=1; i<10; i++)
{
Document.write(“hello world”)
}
• While loop
While(condition)
{
Document.write(“hello world”)
}
Exercise Questions
Exercise Questions