Jump to content

JavaScript/Basics

From IdeaWazaWiki
Revision as of 09:46, 29 May 2007 by wikademia>Hampshire2004 (Clarification on variable typing)

Basic JavaScript is a course under the topic of JavaScript.

What you should know - HTML/XHTML

Basics -

  • JavaScript is the most popular web page scripting language and is suppported by almost every browser.
  • It adds interactivity to your web pages.
  • You can use only the tools already on your computer without the need to download any software.
  • It is easy and fun

First Lesson

Open up your text editor of choice and create a basic page. Now I am going to teach you how to write on your webpage using JavaScript.

<html>
	<body>
		<script type="text/javascript">
			document.write("Hello World!")
		</script>
	</body>
</html>

As you can see, JavaScript is added directly into your HTML by adding the <script> tag. You don't have to understand exactly how that worked, I'm about to explain.

To start off with, the <script type"text/javascript"> tag tells the browser that whatever comes between that tag and the coming </script> tag is JavaScript. Similarly, to use VBScript, you might add a <script type="text/vbscript"> tag. For the purposes of this course, though, you should only ever need to use the JavaScript tag.

It is also important to note that JavaScript is case sensitive. What this means is what ever you type must be in capitalized the way it should be. For example, if you wrote

<ScRiPt TyPe="TeXt/JaVaScRiPt">

not only would you waste a lot of time typing, but you would also confuse your browser.
You are probably wondering when I'm going to explain the rest of what you did. Well, document.write is the JavaScript standard for writing text to the browser window. The 'document' clause refers to the HTML webpage (termed a document) as a whole; what follows ('.write') is a command for the document object to carry out. In this case, you told the page to write the classic "Hello World" to the screen.
These are the absolute essentials to understand for starting JavaScript.
--Xora K Joken 19:18, 3 April 2007 (UTC)


Second Lesson

This second lesson will introduce the topics of variables and operators.

Variables

One of the things JavaScript is used for is the manipulation of data. In JavaScript, pieces of data can either be stored in variables or arrays.

Variables are declared using the var keyword:

var numValue;
var textValue;
var binaryValue;

and can be given values when they are created:

var numValue = 3;
var textValue = "This is text.";
var binaryValue = true;

Multiple variables can even be declared at the same time:

var numValue = 3, textValue = "This is text.", binaryValue = true;

Variables in JavaScript are weakly typed, meaning that the types of individual variables are not determined by the programmer. Unlike many languages, JavaScript only provides a generic var rather than separate types for integers, floating point numbers, characters, and strings. For example, in the statement

var x = 0;

it is fairly obvious to any individual reading the code that x is a number. However, if the statement

x = x + "This is text that is probably not a number";

were to appear later in the code, JavaScript would change the variable to a text variable. This is one of the reasons that JavaScript is referred to as weakly typed; at any particular point in time, it is impossible to tell exactly what type a particular variable is

Arrays

Operators