Software to write JavaScript

an article added by: Jacques Goudreau at 04082007


In: Categories » Computers and technology » Javascript » Software to write JavaScript

You don’t need any special software to write JavaScript. All you need is a plain text editor and a web browser. Code written in JavaScript must be executed from a document written in (X)HTML. There are two ways of doing this. You can place the JavaScript between <script> tags within the <head> of the document:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<script type="text/javascript">
JavaScript goes here...
</script>
</head>
<body>
Mark-up goes here...
</body>
</html>

If you’d like to try the examples in this chapter, go ahead and create two files in a text editor. First, create a simple bare-bones HTML or XHTML file. You can call it something like test.html. Make sure that it contains a <script> tag in the <head> that has a src attribute with a value like example.js. That’s the second file you can create in your text editor. Your test.html file should look something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Just a test</title>
<script type="text/javascript" src="example.js">
</script>
</head>
<body>
</body>
</html>

You can copy any of the examples in this chapter and write them into example.js. None of the examples are going to be particularly exciting, but they may be illuminating. In later chapters, I’ll be showing you how to use JavaScript to alter the behavior and content of your document. For now, I’ll be using simple dialog boxes to display messages. Whenever you change the contents of example.js, you can test its effects by reloading test.html in a web browser. The web browser will interpret the JavaScript code immediately. Programming languages are either interpreted or compiled. Languages like Java or C++ require a compiler. A compiler is a program that translates the source code written in a high-level language like Java into a file that can be executed directly by a computer. Interpreted languages don’t require a compiler—they just need an interpreter instead. With JavaScript, in the context of the World Wide Web, the web browser does the interpreting. The JavaScript interpreter in the browser executes the code directly from the source. Without the interpreter, the JavaScript code would never get executed. If there are any errors in the code written in a compiled language, those errors will pop up when the code is compiled. In the case of an interpreted language, errors won’t become apparent until the interpreter executes the code. Although compiled languages tend to be faster and more portable than interpreted languages, they often have a fairly steep learning curve. One of the nice things about JavaScript is that it’s relatively easy to pick up.

Don’t let that fool you though: JavaScript is capable of some pretty complex programming operations. For now, let’s take a look at the basics.

 

Syntax in JavaScript

English is an interpreted language. By reading and processing these words that I have written in English, you are acting as the interpreter. As long as I follow the grammatical rules of English, my writing can be interpreted correctly. These grammatical rules include structural rules known as syntax. Every programming language, just like every written language, has its own syntax. JavaScript has a syntax that is very similar to that of other programming languages like Java and C++.

 

Statements in JavaScript

A script written in JavaScript, or any other programming language, consists of a series of instructions. These are called statements. These statements must be written with the right syntax in order for them to be interpreted correctly. Statements in JavaScript are like sentences in English. They are the building blocks of any script. Whereas English grammar demands that sentences begin with a capital letter and end with a period, the syntax of JavaScript is much more forgiving. You can simply separate statements by placing them on different lines: first statement second statement If you place a number of statements on the same line, you must separate them with semicolons like this: first statement; second statement; However, it is good programming practice to place a semicolon at the end of every statement even if they are on different lines: first statement; second statement; This helps to make your code more readable. Putting each statement on its own line makes it easier to follow the sequence that your JavaScript is executed in.

 

Comments in JavaScript

Not all statements are (or need to be) executed by the JavaScript interpreter. Sometimes you’ll want to write something purely for your own benefit, and you’ll want these statements to be ignored by the JavaScript interpreter. These are called comments. Comments can be very useful when you want to keep track of the flow of your code. They act like sticky notes, helping you to keep track of what is happening in your script. JavaScript allows you to indicate a comment in a number of different ways. For example, if you begin a line with two forward slashes, that line will be treated as a comment:

// Note to self: comments are good.

If you use this notation, you must put the slashes at the start of each comment line. This won’t work, for instance:

// Note to self:
comments are good.

Instead, you’d need to write:

// Note to self:
// comments are good

. If you want to comment out multiple lines like that, you can place a forward slash and an asterisk at the start of the comment block and an asterisk and forward slash at the end: /* Note to self: comments are good */ This is useful when you need to insert a long comment that will be more readable when it is spread over many lines.

 

Variables in JavaScript

In our everyday lives there are some things about us that are fixed and some things that are changeable. My name and my birthday are fixed. My mood and my age, on the other hand, will change over time. The things that are subject to change are called variables. My mood changes depending on how I’m feeling. Suppose I had a variable with the name mood. I could use this variable to store my current state of mind. Regardless of whether this variable has the value “happy” or “sad”, the name of the variable remains the same: mood. I can change the value as often as I like. Likewise, my age might currently be 33. In one year’s time, my age will be 34. I could use a variable named age to store how old I am and then update age on my birthday. When I refer to age now, it has the value 33. In one year’s time, the same term will have the value 34. Giving a value to a variable is called assignment. I am assigning the value “happy” to the variable mood. I am assigning the value 33 to the variable age. This is how you would assign these variables in JavaScript:

mood = "happy";
age = 33;

legal notice

Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.

Useful tools and features

Link to this article from your page    Send this article to you or to a friend
If you like this article (tutorial), please link to it from your web page using the information above.