Professional Documents
Culture Documents
JavaScript
Tutorial:
Variables and
Operators
Explained
by Jon Perry
JavaScript To such as Java or C++, and allows direct access to the HTML
Control Forms document. Its advantages over these languages are that it
is simple to write and simple to deploy. It doesn't need
anything special to be added to a browser, and doesn't
● JavaScript need any specialist software for its creation.
Tutorial:
Variables and In fact, if you have a Text Editor and a browser, you can
Operators write some JavaScript right now.
Explained
JavaScript is contained within an HTML document. For
example:
● Ask the
JavaScript
<HTML>
Weenie
<HEAD>
<TITLE>My First JavaScript Program</TITLE>
● JavaScript
<SCRIPT>
Discussion
document.write('My First JavaScript Program');
Forum
</SCRIPT>
</HEAD>
</HTML>
● JavaScript
Mailing List When you save this, remember to save it with an .htm or
.html extension.
Find out in: The JavaScript is contained within the <SCRIPT> tags.
Amazing HTML When the browser interprets this document, it thinks
'Hmmm, this is an HTML document, titled 'My First
JavaScript program', including some JavaScript -
'document.write ('My First JavaScript Program')'. Well, I
Site Map better execute this. And so it does.
Object-Orientated
Check out our Web- An object-orientated programming language (OOPL) is a
based system of keywords that perform varying actions. These
Discussion Groups:
keywords interact with internal data structures, known as
Web Development
objects, and with the computer itself.
Yak & Learn
The name object-orientated may throw you a bit. To
Check out and join our
understand it, stop what you are doing for a while and
email-based Mailing
Lists for Web
take a look around at reality. You are quite willing to
developers.
accept reality in terms of objects; there are people, cars,
computers, books, chairs, shoes, pens, haircuts, birds, etc.
Your Email
Each of these objects can be further divided, there are tall
Join List people, short people, red cars, blue cars, PCs and Macs,
and so on.
Developer Each of these objects does things. People talk or walk, cars
Channel go forwards or backwards, birds fly, etc.
FlashKit
Gif.com So, logically, when we try to model reality on a computer,
Jobs.webdeveloper we create objects.
JavaBoutique
The characteristics of an object are called properties, for
JavaScript.com
example (height, color, make), and the specific nature of
JavaScriptSource
each property is called a value (tall, red, PC).
JustSMIL
ScriptSearch The actions of an object are called methods (talk, drive,
Streaming Media fly).
World
VoiceXML Planet OOPLs also involve events. An event is something that
WDVL happens, so for example, a bird could land on a branch or
WebDeveloper.com a haircut could change.
WebReference
XML101 Variables
The most basic computer programming term is a variable.
A variable can hold whatever we want it to.
myHeight='6 foot';
money=100;
So,
<HTML>
<HEAD>
<TITLE>JavaScript Variables</TITLE>
<SCRIPT>
money=100;
document.write('You have '+money+' dollars to spend.');
</SCRIPT>
</HEAD>
</HTML>
We could write:
myHeight='6 foot';
myHeight=7;
money=100;
money='Fifty Quid';
is also legal.
<HTML>
<HEAD>
<TITLE>Changing Variables</TITLE>
<SCRIPT>
money=100;
document.write('You have '+money+' dollars to
spend.<BR>');
document.write('You spend 50 dollars.<BR>');
money=50;
document.write('You have '+money+' dollars to spend.');
</SCRIPT>
</HEAD>
</HTML>
<HTML>
<HEAD>
Operators
An operator acts upon a variable and changes its value.
This offers more control than manually setting a variable
ourselves.
Basic Operators
+ Addition/String addition
- Subtraction
* Multiplication
/ Division
% Modulus
<HTML>
<HEAD>
<TITLE>Money Manager</TITLE>
<SCRIPT>
money=100;
document.write('You have '+money+' dollars to
spend.<BR>');
spend=50;
money=money-spend;
document.write('You spend '+spend+' dollars.<BR>You
have '+money+' dollars to spend.');
</SCRIPT>
</HEAD>
</HTML>
<HTML>
<HEAD>
<TITLE>Message Maker</TITLE>
<SCRIPT>
greetings='Hello';
name='Jon';
document.write(greetings+' '+name);
</SCRIPT>
</HEAD>
</HTML>
<HTML>
<HEAD>
<TITLE>Divide</TITLE>
<SCRIPT>
x=10;
y=3;
document.write(x/y+'<BR>'+x%y);
</SCRIPT>
</HEAD>
</HTML>
Compacted Operators
money-=spend;
This code means exactly the same as:
money=money-spend;
Pre-/Post- Operators
<HTML>
<HEAD>
<TITLE>Pre/Post Operators</TITLE>
<SCRIPT>
x=10;
y=10;
document.write('x is '+x+++', and now x is '+x);
document.write('<BR>y is '+--y+', and now y is '+y);
</SCRIPT>
</HEAD>
</HTML>
Confusing, hey?
Comparison Operators
<HTML>
<HEAD>
<TITLE>Boolean</TITLE>
<SCRIPT>
document.write(4>5);
</SCRIPT>
</HEAD>
</HTML>
Logical Operators
internet.com
Internet News Internet Investing Internet Technology Windows Internet Tech. Linux/Open
Source Web Developer ECommerce/Marketing ISP Resources ASP Resources Wireless
Internet Downloads Internet Resources Internet Lists International EarthWeb Career
Resources Search internet.com Advertising Info Corporate Info Newsletters E-mail
Offers
internet.commerce
">Be a Commerce Partner Windows Web Hosting MLM Leads Submit Your Site AIIM
Exhibits-Free Build Flash Online Software Store Free Search Traffic
Copyright 2003 Jupitermedia Group, Inc. All Rights Reserved. Legal Notices, Licensing, Reprints, & Permissions, Privacy
Policy.