Javascript Prefi
Javascript Prefi
What is Javascript
• JavaScript is a programming language for use
in HTML pages
• Invented in 1995 at Netscape Corporation
(LiveScript) invented by Brendan Eich i
• JavaScript has nothing to do with Java
Why Study JavaScript?
mJavaScript is one of the 3 languages all web
developers must learn:
• 1. HTML to define the content of web pages
• 2. CSS to specify the layout of web pages
• 3. JavaScript to program the behavior of
web pages
Capabilities of JavaScript
• JavaScript Can Change HTML Content
• One of many JavaScript HTML methods
is getElementById().
• This example uses the method to "find" an
HTML element (with id="demo") and changes
the element content (innerHTML) to "Hello
JavaScript":
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick='document.getElementById("demo").innerHTML
= "Hello JavaScript!"'>Click Me!</button>
</body>
</html>
JavaScript Can Change HTML Styles
(CSS)
• Changing the style of an HTML element, is a variant of
changing an HTML attribute:
<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can change the style of an
HTML element.</p>
<button type="button"
onclick="document.getElementById('demo').style.fontSiz
e='35px'">Click Me!</button>
</body>
</html>
JavaScript Can Hide HTML Elements
• Hiding HTML elements can be done by changing the
display style:
<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can hide HTML elements.</p>
<button type="button"
onclick="document.getElementById('demo').style.display
='none'">Click Me!</button>
</body>
</html>
JavaScript Can Show HTML Elements
• Showing hidden HTML elements can also be done by
changing the display style:
<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p>JavaScript can show hidden HTML elements.</p>
<p id="demo" style="display:none">Hello JavaScript!</p>
<button type="button"
onclick="document.getElementById('demo').style.display
='block'">Click Me!</button>
</body>
</html>
Where to put a JavaScript in a
Webpage
• The <script> Tag
• In HTML, JavaScript code must be inserted between
<script> and </script> tags.
• <script>
document.getElementById("demo").innerHTML = "My
First JavaScript";
</script>
JavaScript in <head> or <body>
• You can place any number of scripts in an HTML document.
• Scripts can be placed in the <body>, or in the <head> section of an
HTML page, or in both.
• <!DOCTYPE html>
<html><head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph
changed.";}
</script>
</head>
• <body>
• <h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
• </body>
</html>
JavaScript in <body>
<!DOCTYPE html>
<html>
<body>
<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph
changed.";
}
</script>
</body>
</html>
JavaScript Output
• JavaScript can "display" data in different ways:
– Writing into an HTML element, using innerHTML.
– Writing into the HTML output
using document.write().
– Writing into an alert box, using window.alert().
– Writing into the browser console,
using console.log().
Using innerHTML
• To access an HTML element, JavaScript can
use
the document.getElementById(id) method.
• The id attribute defines the HTML element.
The innerHTML property defines the HTML
content:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5
+ 6;
</script>
</body>
</html>
Using document.write()
• For testing purposes, it is convenient to
use document.write():
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>
• Using document.write() after an HTML document
is fully loaded, will delete all existing HTML:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<button onclick="document.write(5 + 6)">Try
it</button>
</body>
</html>
Using window.alert()
• You can use an alert box to display data:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
Using console.log()
• For debugging purposes, you can use
the console.log() method to display data.
<!DOCTYPE html>
<html>
<body>
<script>
console.log(5 + 6);
</script>
</body>
</html>
JS Statements
• In HTML, JavaScript statements are "instructions" to be
"executed" by the web browser.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p>In HTML, JavaScript statements are executed by the
browser.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello
Dolly.";
</script>
</body>
</html>
JavaScript Programs
• Most JavaScript programs contain many
JavaScript statements.
• The statements are executed, one by one, in
the same order as they are written.
• In this example x, y, and z are given values,
and finally z is displayed:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p>JavaScript code (or just JavaScript) is a sequence of
JavaScript statements.</p>
<p id="demo"></p>
<script>
var x, y, z;
x = 5;
y = 6;
z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Semicolons
Semicolons separate JavaScript statements.
• Add a semicolon at the end of each executable
statement:
• var a, b, c;
a = 5;
b = 6;
c = a + b;
• When separated by semicolons, multiple
statements on one line are allowed:
• a = 5; b = 6; c = a + b;
JavaScript White Space
var x = 10;
x += 5;
Output: 15
JavaScript Data Types
• JavaScript variables can hold numbers like 100
and text values like "John Doe".
• In programming, text values are called text
strings.
• JavaScript can handle many types of data, but for
now, just think of numbers and strings.
• Strings are written inside double or single quotes.
Numbers are written without quotes.
• If you put a number in quotes, it will be treated as
a text string.
• var pi = 3.14;
var person = "John Doe";
var answer = 'Yes I am!';
Declaring JS VARIABLES
<p id="demo"></p>
<script>
var carName = "Volvo";
document.getElementById("demo").innerHTML
= carName;
</script>
One Statement, Many Variables
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
var x = 5;
document.getElementById("demo").innerHTML = (x == 8);
</script>
</body>
</html>
Logical Operators
• Logical operators are used to determine the
logic between variables or values.
• Given that x = 6 and y = 3, the table below
explains the logical operators:
Conditional (Ternary) Operator
• JavaScript also contains a conditional operator
that assigns a value to a variable based on
some condition.
• Syntax
variablename = (condition) ? value1:value2
• var voteable = (age < 18) ? "Too young":"Old
enough";
Output: Old enough
JavaScript If...Else Statements
• Conditional statements are used to perform different
actions based on different conditions.
Very often when you write code, you want to perform
different actions for different decisions.
• You can use conditional statements in your code to do this.
• In JavaScript we have the following conditional statements:
• Use if to specify a block of code to be executed, if a
specified condition is true
• Use else to specify a block of code to be executed, if the
same condition is false
• Use else if to specify a new condition to test, if the first
condition is false
• Use switch to specify many alternative blocks of code to be
executed
Use the if statement to specify a block of
JavaScript code to be executed if a condition is
true.
• if (hour < 18) {
greeting = "Good day";
}
Output: Good Day
The else Statement
• if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
• Output: Good evening
The else if
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
Output: Good evening
JavaScript Switch Statement
• The switch statement is used to perform different
actions based on different conditions.
• Use the switch statement to select one of many blocks
of code to be executed.
switch(expression) {
case n:
code block
break;
case n:
code block
break;
default:
code block
}
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
Common Code Blocks
• switch (new Date().getDay()) {
case 4:
case 5:
text = "Soon it is Weekend";
break;
case 0:
case 6:
text = "It is Weekend";
break;
default:
text = "Looking forward to the Weekend";
}
JS FUNCTIONS
• A JavaScript function is a block of code designed to
perform a particular task.
• A JavaScript function is executed when "something"
invokes it (calls it).
<script>
function myFunction(p1, p2) {
return p1 * p2;
}
document.getElementById("demo").innerHTML =
myFunction(4, 3);
</script>
JavaScript Function Syntax
<script>
function myFunction(a, b) {
return a * b;
}
document.getElementById("demo").innerHTML =
myFunction(4, 3);
</script>
JavaScript Popup Boxes
• JavaScript has three kind of popup boxes: Alert box,
Confirm box, and Prompt box.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Alert</h2>
<script>
alert("I am an alert box!");
</script>
</body>
</html>
Confirm Box
• A confirm box is often used if you want the
user to verify or accept something.
• When a confirm box pops up, the user will
have to click either "OK" or "Cancel" to
proceed.
• If the user clicks "OK", the box returns true. If
the user clicks "Cancel", the box returns false.
<h2>JavaScript Confirm Box</h2>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var txt;
if (confirm("Press a button!") == true) {
txt = "You pressed OK!";
} else {
txt = "You pressed Cancel!";
}
document.getElementById("demo").innerHTML = txt;
}
</script>
Prompt Box
• A prompt box is often used if you want the
user to input a value before entering a page.
• When a prompt box pops up, the user will
have to click either "OK" or "Cancel" to
proceed after entering an input value.
• If the user clicks "OK" the box returns the
input value. If the user clicks "Cancel" the box
returns null.
<h2>JavaScript Prompt</h2>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var txt;
var person = prompt("Please enter your name:", "Harry
Potter");
if (person == null || person == "") {
txt = "User cancelled the prompt.";
} else {
txt = "Hello " + person + "! How are you today?";
}
document.getElementById("demo").innerHTML = txt;
}
</script>
Line Breaks
• To display line breaks inside a popup box, use
a back-slash followed by the character n.
• alert("Hello\nHow are you?");
JavaScript For Loop