This is an introduction/overview of JavaScript. It is written for web designers, not necessarily programmers – so some things will be treated at a higher level.
JavaScript is an interpreted language meaning that it stays in the plain text format, and lets the browser run it each time it encounters it. It means it is a little slower to run, but is faster to program it.
JavaScript needs to be in a script tag, or external file. Generally, the file has a .js extension.
If you want an external file, you still need to use a script tag, but that is to reference the external file:
Learning the Language
JavaScript is in the same family of languages is C/C++, C#, Java, and many others. This means you may see code that looks similar, but not quite right. However, know that while it looks similar, it is also very different.
Each command should be on a separate line. It is not required, but is considered a best practice. Likewise, it is best practice to put a semi-colon (;) at the end of each command. This reduces the chance of errors in some cases.
JavaScript can be used to create a simple pop up message. It can be done with the alert command, as shown below:
alert('Hello world');
Comments
Comments are ways to excluding sections of your javascript from running. YOu might ask why would you do this? There are a couple of reasons:
- Because you want to exclude some old code that you no longer need, but may want later
- Because you want to exclude code that you think may be breaking your program
- Because you want to leave a note about what you are doing.
There are two types of comments, single line, and multi-line.
With Single Line comments, you use two forward slashes, and the comment ends at the end of that line.
With Multi-Line Comments, you use a /* to start it, and a */ to end it. It can be over as many lines as you want. It can also be used to just comment out part of a line of code. See the examples below:
// Example of a Single Line Comment /* This is a multi line comment */ /* So is this */
Variables
You can also define a variable as a run time storage device. This will store information over the long-term so it can be accessed again later.
Variables are made up of a couple of pieces of information. First is the name that you call it. The second is the data that makes it up. JavaScript does not require you to define a data type. This will make it much easier for you initially – however, it can also cause problems later on. So always check to see if you are putting a different types values into the same variable.
Examples of some JavaScript variables:
var userName = "Tom"; // this is an example of a string // stings are enclosed with either a single quote or a double quote var age = 25; // This is an integer number var height = 6.167; // This is a floating point number var today = new Date(); // Date is a built-in object for working with dates.
Basic Math
JavaScript, like most languages, allow you to do basic math, including addition (+), subtraction (-), multiplication (*), and division (/).
When you do math, you can use variables to hold values (like in algebra), and also store, or display the results.
alert( 5 + 7 ); var x = 5; var y = 7; var z = 0; alert( x * y ); z = x - y; alert(z);
More examples and information will be available in Intro to JavaScript Part II of this topic.
An Introduction to JavaScript was originally found on Access 2 Learn
2 Comments
Comments are closed.