JS Syntax & Statements

1. JavaScript is a programming language

  • A computer program is a list of "instructions" to be "executed" by the computer.
    • In a programming language, these program instructions are called statements.
    • In HTML, JavaScript programs can be executed by the web browser.
  • JS statements 用 semicolons(分號) 或換行區隔
    • Ending statements with semicolon is not required, but highly recommended.
    • It is a default JavaScript behavior to close a statement automatically at the end of a line.
      • 有些語法支援拆成兩行以上 (e.g., var),有些不行 (e.g., return),會有預期外的結果
      • 原理:如果語法是不完整的,JS會嘗試繼續去讀下一行語法來完整它。但 return 本身可以自成完整語法,JS只會自動幫忙加上分號
    • function 宣告的結尾通常不加分號
      • Since a function declaration is not an executable statement, it is not common to end it with a semicolon.
// it's fine
var
x = 10;
// it does not work as expected. return `undefined`
function myFunction(a) {
    return
    a * 10;
}

// because it will be considered as below:
function myFunction(a) {
    return;
    a * 10;
}

2. JavaScript Statements are composed of

2.1. Values

The JavaScript syntax defines two types of values:

  • Fixed values:
    • are called literals(實字).
    • Numbers are written with or without decimals. EX: 10.50, 1001
    • Strings are text, written within double or single quotes. EX: "Hi", 'Hi'
  • Variable values:

    • are called variables.
    • In a programming language, variables are used to store data values.
    • JavaScript uses the var keyword to declare variables.

      EX: var x;

2.2. Operators

  • assignment operator ( =, a.k.a. equal sign ) to assign values to variables.

    EX: var x = 5;

  • arithmetic operators ( + - * / ) to compute values.

    EX: 5 + 6 * 10

2.3. Expressions

  • An expression is a combination of values, variables, and operators, which computes to a value.
  • The computation is called an evaluation.

    EX1: 5 * 10 (evaluates to 50)

    EX2: x * 10

    EX3: "John" + " " + "Doe"

    EX4: ( 10 > 9 ) (evaluates to true)

2.4. Keywords

  • E.g., var, return, continue, break, if...else...
  • JavaScript keywords are used to identify actions to be performed.
  • JavaScript keywords are reserved words. Reserved words cannot be used as names for variables.
  • JavaScript keyword list

2.5. Comments

  • 註解,單行用 //,多行用 /* and */
//it's a comment
/*it's a comment block*/
  • It is most common to use single line comments.
  • Block comments are often used for formal documentation.

3. JavaScript is Case Sensitive

  • All JavaScript identifiers are case sensitive.
    • E.g., VAR or Var does not be interpreted as the keyword var.
    • E.g., lastName and lastname, are two different variables.

4. 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;

5. JavaScript Character Set

  • JavaScript uses the Unicode character set.
  • Unicode covers (almost) all the characters, punctuations, and symbols in the world.
  • For a closer look, please study our Complete Unicode Reference.

6. JavaScript Line Length and Line Breaks

  • For best readability, programmers often like to avoid code lines longer than 80 characters.
  • 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.";

results matching ""

    No results matching ""