Javascript1
Jump to navigation
Jump to search
Course Outline
Session 1
- Firebug Console/Interpreter
- Expressions
An expression (EXPR) can be:
- a primitive (e.g. 1)
- a variable (e.g. x)
- an operator combining smaller EXPR's (e.g. x+1, 2*(x+1))
- Operators have:
- keyword or symbol (OP)
- syntax template (e.g. _ OP _)
- prefix: OP_
- postfix: _OP
- infix: _OP_
- compound: _OP_OP_
- one or more inputs _ ("operands")
- one output ("return value")
- may be undefined
- possible side-effects (e.g. change in variable's value)
- Variables have:
- a name/identifier
- a value (initially undefined)
- each value has a type (primitive or reference)
- a scope
- Other (non-expression) Statements
- var NAME;
- var NAME = EXPR;
- if (EXPR) {EXPR;}
- if (EXPR) {EXPR;} else {EXPR;}
- Primitive Types
- undefined
- Number
- same type for integers and floats (fractional)
- special values: NaN ("not a number"), Infinity
- String
- wrapped in either " " or ' '
- may contain opposite quote or other special characters
- \t (tab)
- \n (newline)
- Boolean (true/false)
- false-ish values: false,undefined,null,0,"",NaN
- (some) true-ish values: true,1,"false","undefined","0",{} (empty object)
Some Sample Operators:
operator | input type | example | result | result type | side effects |
---|---|---|---|---|---|
typeof | NUM | typeof 1 | "number" | STR | |
STR | typeof "word" | "string" | |||
BOOL | typeof true | "boolean" | |||
,
(sequence) |
any | "a", "b", "c" | "c" | type of
last value | |
(4,"3"),("two",1) | 1 | ||||
+
(plus) |
NUM | 1+1 | 2 | NUM | |
+
(concatenate) |
STR | "1"+"1" | "11" | STR | |
mixed | 1+"2" | "12" | STR | ||
- * / % | NUM | 3-1 | 2 | NUM | |
STR | "3"-"1" | 2 | |||
mixed | "3"-1 | 2 | |||
< > <= >=
(comparison) |
NUM | 1<2 | true | BOOL | |
STR | "b"<"a" | false | |||
==
(equality) |
NUM | 1==2 | false | BOOL | |
STR | "a"=="aa" | false | |||
mixed | 1=="1" | true | |||
mixed | 0==false | true | |||
===
(identity) |
NUM | 1===2 | false | BOOL | |
STR | "abc"==="abc" | true | |||
mixed | 1==="1" | false | |||
mixed | 1===true | false | |||
!
(not) |
any | !1 | false | BOOL | |
!"" | true | ||||
&&
(and) |
any | null && "red" | null | first false-ish
or last input | |
1 && "green" | "green" | ||||
||
(or) |
any | "yes" || null | "yes" | first true-ish
or last input | |
"" || 0 | 0 | ||||
variable assignment operators | x is now... | ||||
=
(assignment) |
any | x=1 | 1 | same as input | 1 |
x = "blue" | "blue" | "blue" | |||
+=
(incremental assignment) |
NUM | x=0; x+=2; | 2 | NUM | 2 |
STR | x="he"; x+="llo"; | "hello" | STR | "hello" | |
mixed | x=""; x+=2; | "2" | STR | "2" | |
++
(post-increment) |
NUM | x=0; x++; | 0 | NUM | 1 |
STR | x=""; x++; | 0 | 1 | ||
++
(pre-increment) |
NUM | x=0; ++x; | 1 | NUM | 1 |
STR | x=""; ++x; | 1 | 1 |