Javascript Basic Syntax

1. Basic rules

  • JavaScript is case sensitive
    • The variables firstName and firstname, are two different variables.
    • The functions myFunction() and myfunction(), are two different functions.
  • JavaScript uses the Unicode character set.
    • It is highly recommended that you should use UTF-8 as encoding type.

2. Literals

1) What is “literals”?
  • notation sets for representing a fixed value in source code.
2) Number literals

  • Infinity
    • It represents a number too big

  • NaN
    • It represents “not a number”

3) String literals

4) Expression literals

4) Array literals

5) Object literals

6) Function literals

3. Variables

1) What is variables?
  • Named variables store data values.
  • A variable is a name. A literal is value.
  • JavaScript usually uses the var keyword to define variables

2) Local variables vs. global variables
  • Variables declared outside a Javascript function become GLOBAL, and all scripts and functions can access it.
  • A variable declared within a JavaScript function becomes LOCAL and can only be accessed within that function.
  • If you assign values to variables without “var” regardless of its location at codes, the variables will automatically be declared as GLOBAL.
  • In node.js, you can find ‘global’ object (variable) as well as ‘GLOBAL’ object (variable).
    • GLOBAL is an alias of global

3. Operators

1) Arithmetic and assignment operators
  • Most arithmetic and assignment operators and their usage rules are similar to the ones of Java or C.
    • +, -, *, /, %, ++, —
    • =, +=, -=, *=, /=, %=
  • Here are some examples worth reading and executing.

2) Comparison operators
  • Most comparison operators and their usage rules are similar to the ones of Java or C.
    • ==, === ,!=, >, <, >=, <=
  • But, it is worth nothing that there are two “equality” tests in Javascript.
    • A == B: Value of A is equal to value of B
    • A === B: Value and type of A are equal to value and type of B

3) Logical operators
  • Most logical operators and their usage rules are similar to the ones of Java or C.
    • &&, ||, !
  • Lazy evaluation

4) Bitwise operators
  • Most bitwise operators and their usage rules are similar to the ones of Java or C.
    • &, |, !, ^, <<, >>
5) Unary operators
  • Unary + operator and – operator can be used to convert a variable to a number.
  • If the type conversion fails, it will still become a number, but with the value NaN (Not a number):

6) typeof operator
  • returns a string that represents the data type
  • return values
    • “number”, “string”, “boolean”, “undefined”, “object”, or “function“
  • Infinity
    • It represents a number too big
    • “typeof Infinity” returns “number”
  • NaN
    • It represents “not a number”
    • “typeof NaN” returns “number”

4. JavaScript Comments

  • // – single line comment
  • /* … */ – multiple lines comment

5. JavaScript Control Statements

1) if
  • Basic syntax

  • Checking if a variable exists

  • Alternative if Syntax

2) switch .. case
  • Basic syntax

  • String matching is possible.

3) for

4) while

5) do ~ while

6) break

7) continue

8) for ~ in

9) try ~ catch

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see

%d 블로거가 이것을 좋아합니다: