JavaScript Is The Programming Language of HTML and The Web
JavaScript Is The Programming Language of HTML and The Web
JavaScript in <head>
In this example, a JavaScript
function is placed in the <head>
section of an HTML page.
The function is invoked (called)
when a button is clicked:
<!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>
In this example, a JavaScript
function is placed in the <body>
section of an HTML page.
The function is invoked (called)
when a button is clicked:
Example
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
document.getElementById("demo
").innerHTML = "Paragraph
changed.";
}
</script>
</body>
</html>
External JavaScript
Scripts can also be placed in
external files:
External file: myScript.js
function myFunction() {
document.getElementById("demo
").innerHTML = "Paragraph
changed.";
}
External scripts are practical when
the same code is used in many
different web pages.
JavaScript files have the file
extension .js.
To use an external script, put the
name of the script file in the src
(source) attribute of a <script>
tag:
Example
<!DOCTYPE html>
<html>
<body>
<h2>External JavaScript</h2>
<button type="button"
onclick="myFunction()">Try it</button>
<script src="myScript.js"></script>
</body>
</html>
External JavaScript
Advantages
Placing scripts in external files has
some advantages:
It separates HTML and code
It makes HTML and JavaScript
easier to read and maintain
Cached JavaScript files can
speed up page loads
To add several script files to one
page - use several script tags:
Example
<script src="myScript1.js"></scr
ipt>
<script src="myScript2.js"></scr
ipt>
JavaScript Output
JavaScript Display
Possibilities
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:
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
<p id="demo"></p>
<script>
document.getElementById("demo").
innerHTML = 5 + 6;
</script>
</body>
</html>
Changing the innerHTML property
of an HTML element is a common
way to display data in HTML.
Using document.write()
For testing purposes, it is
convenient to
use document.write():
Example
<!DOCTYPE html>
<html>
<body>
<script>
document.write(5 + 6);
</script>
</body>
</html>
Using document.write() after an
HTML document is loaded,
will delete all existing HTML:
Example
<!DOCTYPE html>
<html>
<body>
<button type="button" onclick="d
ocument.write(5 + 6)">Try
it</button>
</body>
</html>
The document.write() method
should only be used for testing.
Using window.alert()
You can use an alert box to
display data:
Example
<!DOCTYPE html>
<html>
<body>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
Using console.log()
For debugging purposes, you can
use the console.log() method to
display data.
You will learn more about
debugging in a later chapter.
Example
<!DOCTYPE html>
<html>
<body>
<script>
console.log(5 + 6);
</script>
</body>
</html>
JavaScript Statements
Example
var x, y, z; // Statement 1
x = 5; // Statement 2
y = 6; // Statement 3
z = x + y; // Statement 4
JavaScript Programs
A computer program is a list of
"instructions" to be "executed" by
a computer.
In a programming language, these
programming instructions are
called statements.
A JavaScript program is a list of
programming statements.
In HTML, JavaScript programs are
executed by the web browser.
JavaScript Statements
JavaScript statements are
composed of:
Values, Operators,
Expressions, Keywords, and
Comments.
This statement tells the browser to
write "Hello Dolly." inside an HTML
element with id="demo":
Example
document.getElementById("demo").
innerHTML = "Hello Dolly.";
Try it Yourself »
Most JavaScript programs contain
many JavaScript statements.
The statements are executed, one
by one, in the same order as they
are written.
JavaScript programs (and
JavaScript statements) are often
called JavaScript code.
Semicolons ;
Semicolons separate JavaScript
statements.
Add a semicolon at the end of
each executable statement:
var a, b, c; // Declare 3
variables
a = 5; // Assign the
value 5 to a
b = 6; // Assign the
value 6 to b
c = a + b; // Assign the
sum of a and b to c
When separated by semicolons,
multiple statements on one line
are allowed:
a = 5; b = 6; c = a + b;
On the web, you might see
examples without semicolons.
Ending statements with semicolon
is not required, but highly
recommended.
JavaScript Syntax
JavaScript syntax is the set
of rules, how JavaScript programs
are constructed:
var x, y ,z; // How to declare
variables
x = 5; y = 6; // How to
assign values
z = x + y; // How to
compute values
JavaScript Values
The JavaScript syntax defines two
types of values: Fixed values and
variable values.
Fixed values are called literals.
Variable values are
called variables.
JavaScript Literals
The most important rules for
writing fixed values are:
Numbers are written with or
without decimals:
10.50
1001
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Numbers</h2>
<p>Number can be written with or without
decimals.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerH
TML = 10.50;
</script>
</body>
</html>
'John Doe'
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strings</h2>
<script>
document.getElementById("demo").innerH
TML = 'John Doe';
</script>
</body>
</html>
JavaScript Variables
In a programming
language, variables are used
to store data values.
JavaScript uses the var keyword
to declare variables.
An equal sign is used to assign
values to variables.
In this example, x is defined as a
variable. Then, x is assigned
(given) the value 6:
var x;
x = 6;
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<script>
var x;
x = 6;
document.getElementById("demo").innerH
TML = x;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x is defined as a
variable.
Then, x is assigned the value of 6:</p>
<p id="demo"></p>
<h1 id="sample"></h1>
<script>
var x;
x = 6;
y = 7;
z = 10;
result = y + z;
document.getElementById("demo").innerH
TML = x;
document.getElementById("sample").inner
HTML = z;
document.write(result);
</script>
</body>
</html>
JavaScript Operators
JavaScript uses arithmetic
operators ( + - * / )
to compute values:
(5 + 6) * 10
JavaScript uses an assignment
operator ( = ) to assign values
to variables:
var x, y;
x = 5;
y = 6;
JavaScript Expressions
An expression is a combination of
values, variables, and operators,
which computes to a value.
The computation is called an
evaluation.
For example, 5 * 10 evaluates to
50:
5 * 10
Expressions can also contain
variable values:
x * 10
The values can be of various
types, such as numbers and
strings.
For example, "John" + " " +
"Doe", evaluates to "John Doe":
"John" + " " + "Doe"
JavaScript Keywords
JavaScript keywords are used to
identify actions to be performed.
The var keyword tells the browser
to create variables:
var x, y;
x = 5 + 6;
y = x * 10;
JavaScript Comments
Not all JavaScript statements are
"executed".
Code after double slashes // or
between /* and */ is treated as
a comment.
Comments are ignored, and will
not be executed:
var x = 5; // I will be executed
JavaScript Identifiers
Identifiers are names.
In JavaScript, identifiers are used
to name variables (and keywords,
and functions, and labels).
The rules for legal names are
much the same in most
programming languages.
In JavaScript, the first character
must be a letter, or an underscore
(_), or a dollar sign ($).
Subsequent characters may be
letters, digits, underscores, or
dollar signs.
Numbers are not allowed as the
first character.
This way JavaScript can easily
distinguish identifiers from
numbers.
Example:
_asg
$shsi
Shshs1123
Shshs_1234
Example
var x = 5; // Declare x,
give it the value of 5
var y = x + 2; // Declare y,
give it the value of x + 2
Multi-line Comments
Multi-line comments start with /*
and end with */.
Any text between /* and */ will be
ignored by JavaScript.
This example uses a multi-line
comment (a comment block) to
explain the code:
Example
/*
The code below will change
the heading with id = "myH"
and the paragraph with id =
"myP"
in my web page:
*/
document.getElementById("myH").i
nnerHTML = "My First Page";
document.getElementById("myP").i
nnerHTML = "My first
paragraph.";
Example
/*
document.getElementById("myH").i
nnerHTML = "My First Page";
document.getElementById("myP").i
nnerHTML = "My first
paragraph.";
*/
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comments</h2>
<h1 id="myH"></h1>
<p id="myP"></p>
<script>
/*
document.getElementById("myH").innerHT
ML = "Welcome to my Homepage";
document.getElementById("myP").innerHT
ML = "This is my first paragraph.";
*/
document.getElementById("myP").innerHT
ML = "The comment-block is not
executed.";
</script>
</body>
</html>
JavaScript Variables
JavaScript variables are containers
for storing data values.
In this example, x, y, and z, are
variables:
Example
var x = 5;
var y = 6;
var z = x + y;
From the example above, you can
expect:
x stores the value 5
y stores the value 6
z stores the value 11
JavaScript Identifiers
All JavaScript variables must
be identified with unique
names.
These unique names are
called identifiers.
Identifiers can be short names
(like x and y) or more descriptive
names (age, sum, totalVolume).
The general rules for constructing
names for variables (unique
identifiers) are:
Names can contain letters,
digits, underscores, and dollar
signs.
Names must begin with a letter
Names can also begin with $
and _ (but we will not use it in
this tutorial)
Names are case sensitive (y
and Y are different variables)
Reserved words (like JavaScript
keywords) cannot be used as
names
JavaScript identifiers are case-
sensitive.
Example
var pi = 3.14;
var person = "John Doe";
var answer = 'Yes I am!';
Declaring (Creating)
JavaScript Variables
Creating a variable in JavaScript is
called "declaring" a variable.
You declare a JavaScript variable
with the var keyword:
var carName;
After the declaration, the variable
has no value. (Technically it has
the value of undefined)
To assign a value to the variable,
use the equal sign:
carName = "Volvo";
You can also assign a value to the
variable when you declare it:
var carName = "Volvo";
In the example below, we create a
variable called carName and
assign the value "Volvo" to it.
Then we "output" the value inside
an HTML paragraph with
id="demo":
Example
<p id="demo"></p>
<script>
var carName = "Volvo";
document.getElementById("demo").
innerHTML = carName;
</script>
It's a good programming practice
to declare all variables at the
beginning of a script.
One Statement, Many
Variables
You can declare many variables in
one statement.
Start the statement with var and
separate the variables by comma:
var person = "John Doe", carName
= "Volvo", price = 200;
A declaration can span multiple
lines:
var person = "John Doe",
carName = "Volvo",
price = 200;
Value = undefined
In computer programs, variables
are often declared without a value.
The value can be something that
has to be calculated, or something
that will be provided later, like
user input.
A variable declared without a
value will have the
value undefined.
The variable carName will have
the value undefined after the
execution of this statement:
Example
var carName;
You can also add strings, but
strings will be concatenated:
Example
var x = "John" + " " + "Doe";