You are on page 1of 18

JavaScript

A Review
JavaScript
JavaScript is a Scripting Language
A scripting language is a lightweight
programming language. JavaScript is
(relatively) easy to learn.
JavaScript code can be inserted into
any HTML page, and it can be
executed by all types of web browsers.
JavaScript
A script is usually written as part of an
HTML document and it controls the
elements of a page and reacts to user
actions.
Error: Can not process
Love me request.
Love me

It uses simple commands which


manipulate the parts of a web page.
Question:

JavaScript Java
(CMSC 2 and CMSC 100) (CMSC 22)
JavaScript
JavaScript has very little
to do with the
programming language
named Java.

The similar name was


inspired by marketing
considerations, rather
than good judgement.
JavaScript
JavaScript is usually used for the
following purposes:
Customizing a page based on the
user's browser version
Providing visual feedback to user
actions
Checking for mistakes in forms before
they are submitted
JavaScript
JavaScripts in HTML must be inserted
between <script> and </script>
tags.

JavaScripts can be put in the <body>


and in the <head> section of an HTML
page.

script
JavaScript
<script>
put your javascript
code here
</script>
<script type="text/javascript">
put your javascript
code here
</script>
JavaScript
<body>
<head>
<script>
put your javascript
code here
</script>
</head>
</body>

<script src="myScript.js"></script>
JavaScript
Manipulating HTML Elements
To access an HTML element from
JavaScript, you can use the
document.getElementById(id)
method.
Use the "id" attribute to identify the
HTML element
JavaScript
<body>
<p id="demo">My First Paragraph.</p>
<script>
document.getElementById("demo").
innerHTML="My First JavaScript";
</script>
</body>
JavaScript
<body>
<script>
document.getElementById("demo").
innerHTML="My First JavaScript";
</script>
<p id="demo">My First Paragraph.</p>
</body>
JavaScript
JavaScript Statements
JavaScript statements are
"commands" to tell the browser what
to do.
It is good practice to put a semicolon
at the end of your javascript
statements.
document.getElementById("demo").innerHTML="My First JavaScript";
JavaScript
JavaScript is Case Sensitive
getElementById is not the
same as getElementbyID.

JavaScript ignores extra spaces.


var num="143"; is same as
var num = "143";
JavaScript
JavaScript Comments
Comments in JavaScript are just like
comments in C.
Comments can be added to explain
the JavaScript, or to make the code
more readable.
JavaScript
Single line comments start with //.
// print an emo name
document.write("<p>Marteer</p>");
Multi line comments start with /* and
end with */.
/*This is an example
Of a multiline comment. This will
Be ignored by the browser.*/
document.write("<p>Marteer</p>");
JavaScript
JavaScript variables are "containers"
for storing information.
Variable names must begin with a letter,
$, or _
Variable names are case sensitive (sum
and Sum are different variables)
var age = 17;
var $age= 17;
var _age= 17;
JavaScript
When you assign a numeric value to a
variable, do not put quotes around the
value.
If you put quotes around a numeric
value, it will be treated as text.
var pi=3.14;
var pie="3.14";
var fruit='buko';

You might also like