JavaScript How To …

출처 (Original Post) : http://www.w3schools.com

Ending Statements With a Semicolon?

With traditional programming languages, like C++ and Java, each code statement has to end with a semicolon.

Many programmers continue this habit when writing JavaScript, but in general, semicolons are optional! However, semicolons are required if you want to put more than one statement on a single line.

Using an External JavaScript

Sometimes you might want to run the same JavaScript on several pages, without having to write the same script on every page.

To simplify this, you can write a JavaScript in an external file. Save the external JavaScript file with a .js file extension.

Note: The external script cannot contain the <script> tag!

To use the external script, point to the .js file in the “src” attribute of the <script> tag:

<html><head><script src="xxx.js"></script></head><body></body></html>

Note: Remember to place the script exactly where you normally would write the script!

JavaScript Variables

Variables

A variable is a “container” for information you want to store. A variable’s value can change during the script. You can refer to a variable by name to see its value or to change its value.

Rules for variable names:

  • Variable names are case sensitive
  • They must begin with a letter or the underscore character

IMPORTANT! JavaScript is case-sensitive! A variable named strname is not the same as a variable named STRNAME!


Declare a Variable

You can create a variable with the var statement:

var strname = some value

You can also create a variable without the var statement:

strname = some value


Assign a Value to a Variable

You can assign a value to a variable like this:

var strname = "Hege"

Or like this:

strname = "Hege"

The variable name is on the left side of the expression and the value you want to assign to the variable is on the right. Now the variable “strname” has the value “Hege”.


Lifetime of Variables

When you declare a variable within a function, the variable can only be accessed within that function. When you exit the function, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different functions, because each is recognized only by the function in which it is declared.

If you declare a variable outside a function, all the functions on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.

JavaScript is Case Sensitive

A function named "myfunction" is not the same as "myFunction"
and a variable named "myVar" is not the same as "myvar".

JavaScript is case sensitive – therefore watch your
capitalization closely when you create or call variables,
objects and functions.


White Space

JavaScript ignores extra spaces. You can add white space to
your script to make it more readable. The following lines are
equivalent:

name="Hege"
name = "Hege"

 


Break up a Code Line

You can break up a code line within a text string with
a backslash. The example below will be displayed properly:

document.write("Hello \
World!")

However, you cannot break up a code line like this:

document.write \
("Hello World!")

 


Comments

You can add comments to your script by using two slashes //:

//this is a comment
document.write("Hello World!")

or by using /* and */ (this creates a multi-line comment
block):

/* This is a comment
block. It contains
several lines */
document.write("Hello World!")