JavaScript Programs(程序)
- A computer program is a list of "instructions" to be "executed" by the computer.
- In a programming language, these program instructions are called statements.(語句)
- JavaScript is a programming language.
- JavaScript statements are separated by semicolon.(分號)
Tips: In HTML, JavaScript programs can be executed by the web browser.
JavaScript Statements(語句)
JavaScript statements are composed of(語句組成):
Values, Operators, Expressions, Keywords, and Comments.
值,運算元,表達式,關鍵字和註釋
JavaScript Values
The JavaScript syntax defines two types of values: Fixed(固定的) values and variable values.
Fixed values are called literals(文字). Variable values are called variables(變量/變數).
JavaScript Literals
The most important rules for writing fixed values are:
- Numbers(數值) are written with or without decimals:
10.50
1001
- Strings(字符串) are text(文本), written within double or single quotes:
"John Doe"
'John Doe'
- Expressions(表達式/表現) can also represent fixed values:
5 + 6
5 * 10
JavaScript Variables
In a programming language, variables are used to store(儲存) data values.
JavaScript uses the var keyword to define variables.(Var來定義變量/數)
An equal sign is used to assign values to variables.(等號用來配置值)
In this example, x is defined as a variable. Then, x is assigned (given) the value 6:
var x;
x = 6;
JavaScript Operators(運算符號)
JavaScript uses an assignment operator ( = ) to assign values to variables:
var x = 5;
var y = 6;
JavaScript uses arithmetic(算術/四則運算) operators ( + - * / ) to compute values:
(5 + 6) * 10 //己是完整的運算式,只是沒有把結果賦予(=)放置到對象/變數。
Tips: In HTML, JavaScript programs can be executed by the web browser.
JavaScript Statements
This statement tells the browser to write "Hello Dolly." inside an HTML element with id="demo":
document.getElementById("demo").innerHTML = "Hello Dolly.";
JavaScript Programs
Most JavaScript programs contain many JavaScript statements.
The statements are executed, one by one, in the same order as they are written.
In this example, x, y, and z is given values, and finally z is displayed:
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML = z;
tips:JavaScript programs (and JavaScript statements) are often called JavaScript code.
Semicolons( ;)
Semicolons separate JavaScript statements.
Add a semicolon at the end of each executable statement:
a = 5;
b = 6;
c = a + b;
When separated by semicolons, multiple statements on one line are allowed:
a = 5; b = 6; c = a + b;
On the web, you might see examples without semicolons.
Ending statements with semicolon is not required, but highly recommended.
語句的結束分號是不必要的,但避免程式解析時的岐異,最好不要省略,自找麻煩!
JavaScript White Space
JavaScript ignores multiple spaces. You can add white space to your script to make it more readable.(忽略多個連續空白間隔)
The following lines are equivalent:
var person = "Hege";
var person="Hege";
A good practice is to put spaces around operators ( = + - * / ):
好習慣:運算符號之間放上空白間隔
var x = y + z;
JavaScript Line Length and Line Breaks
For best readability, programmers often like to avoid code lines longer than 80 characters.
//最佳的可讀性,通常喜歡避免代碼行超過80個字元
If a JavaScript statement does not fit on one line, the best place to break it, is after an operator:
//最好的斷句是在操作符號之後
document.getElementById("demo").innerHTML =
"Hello Dolly.";
JavaScript Code Blocks(代碼塊/分段)
JavaScript statements can be grouped together in code blocks, inside curly brackets {...}.
The purpose of code blocks this is to define statements to be executed together.
One place you will find statements grouped together in blocks, are in JavaScript functions:
function myFunction() {
document.getElementById("demo").innerHTML = "Hello Dolly.";
document.getElementById("myDIV").innerHTML = "How are you?";
}
In this tutorial we use 4 spaces of indentation for code blocks.
JavaScript Keywords
JavaScript statements often start with a keyword to identify the JavaScript action to be performed.
Here is a list of some of the keywords you will learn about in this tutorial:
Keyword | Description |
break | Terminates a switch or a loop |
continue | Jumps out of a loop and starts at the top |
debugger | Stops the execution of JavaScript, and calls (if available) the debugging function |
do ... while | Executes a block of statements, and repeats the block, while a condition is true |
for | Marks a block of statements to be executed, as long as a condition is true |
function | Declares a function |
if ... else | Marks a block of statements to be executed, depending on a condition |
return | Exits a function |
switch | Marks a block of statements to be executed, depending on different cases |
try ... catch | Implements error handling to a block of statements |
var | Declares a variable |
JavaScript keywords are reserved words. Reserved words cannot be used as names for variables.
詳細內容可以參考:
- w3school.com (http://www.w3schools.com/)
-
程式語言教學誌(http://pydoing.blogspot.tw/)
沒有留言:
張貼留言