JavaScript adds simple or sophisticated interactivity to a Web site, enhancing user's experience. Like any programming language, you need to understand building blocks before you can start programming.Start at Beginning
Browsers know to interpret Web pages as HTML because of tags. Since JavaScript is contained inside an HTML document, it needs to be set apart with tags.
TITLE< itle>
Don't forget that last tag! Abrowser will try and interpret whole HTML page as JavaScript, until it comes to that closing tag. Without it, page will generate unsightly errors and not load properly.
Comment, Comment, Comment
Commenting code allows you, or someone else looking at it, to understand what's occuring in code. Commenting can be done in both single and multi-line variations:
// single line comments
/* multi-line comments */
But what about HTML comment inside script tags. That exists so older browsers that don't understand JavaScript won't try and interpret it. Otherwise, code will render page as HTML, resulting in page displaying incorrectly.
Defining Variables
JavaScript, like all programming languages, uses variables to store information. These variables can store numbers, strings, variables that have been previously defined, and objects. For example:
Numeric: var x = 0; String: var y = "hello"; Variables: var z = x + y; Object: var myImage = new Image();
Strings MUST contain "" around word or phrase. Otherwise JavaScript will interpret it as a number. Numbers and previously defined variables, likewise, should not have "" unless you want that number to be treated as a string.
Ex: var x = hello ** wrong
Variables that store numbers and strings can be combined in a new variable. However, if anything is combined with a string, it is automatically be treated as a string.
Ex: var y = "1"; var z = "2"; var a = y + z;
The variable "a" in this instance is "12" not 3, since two strings were combined together as text, not added like numbers. This would be true even if y = 1.
Making a Statement
Notice semi-colons (;) at end of each line of code? The semi-colon denotes end of that particular statement. While JavaScript can sometimes be forgiving if you don't include semi-colon at end of each statement, it's good practice to remember to do so. Otherwise, you might not remember to put it there when you really need it.
Alert! Alert!
Alerts are one of greatest functions in JavaScript. They not only pass information on to visitors, but help you when you're trying to hunt down a bug in your code.
Examples of alerts
alert("this is a string"); creates an alert that will contain text"this is a string"