- Number
- String
- Boolean
- Symbol (new in ES2015)
- Object
- Function
- Array
- Date
- RegExp
- Math
- null
- undefined
Math, a build-in Object.
Math.sin(3.5);
var circumference = 2 * Math.PI * r;
parseInt(), a build-in Function.
parseInt('123', 10); // 123
parseInt('010', 10); // 10
/* convert a binary number to an integer */
parseInt('11', 2); // 3
parseFloat(), a build-in Function, it always use base 10.NaN, is a special value (short for "Not a Number")
parseInt('hello', 10); // NaN
/* NaN is toxic */
NaN + 5; // NaN
isNaN(), a build-in Function.
isNaN(NaN); // true
Infinityand-Infinity, are special value.
1 / 0; // Infinity
-1 / 0; // -Infinity
isFinite(), a build-in function
isFinite(1 / 0); // false
isFinite(-Infinity); // false
isFinite(NaN); // false
lengthof string.
'hello'.length; // 5
- String hava method.
'hello'.charAt(0); // "h"
'hello, world'.replace('hello', 'goodby'); // "goodby, world"
'hello'.toUpperCase(); // "HELLO"
'hello'.toLowerCase(); // "hello"
nullandundefinedare differencefalse,0,empty,String(""),NaN,null,undefinedall becomefalse. All other values becometrue.
Boolean(''); // false
Boolean(234); // true
&&,||,!operator for boolean value.
- define variable use one of three keywords:
let,const,var letdefine block level variable, variable is available from the function block it is enclosed in.
let a;
let name = 'Simon';
scope of let variable
// myLetVar *not* visible here
for (let myLetVar = 0; myLetVar < 5; myLetVar++) {
//myLetVar is only visible in here
}
// myLetVar *not* visible here
const, declare const var which never intend to change
const PI = 3.14;
PI = 1; // will throw an error since can't change const var value
- An important difference between JavaScript and other languages like Java is that in JavaScript, blocks do not have scope; only functions have scope. So if a variable is defined using var in a compound statement (for example inside an if control structure), it will be visible to the entire function. However, starting with ECMAScript 2015,
letandconstdeclarations allow you to create block-scoped variables.
+,-,*,/,%+=,-=,++,--
x += 5;
x = x + 5;
+, also does string concatenation
'hello' + 'world'; // "hello world"
add string to number, everything converted to a string first.
'3' + 4 + 5; // "345"
3 + 4 + '5'; // "75"
- Compare,
<,>,<=,>= ==, perform type coercion.
123 == '123'; // true
1 == true; // true
===, avoid type coercion.
123 === '123'; // false
1 === true; // false
if,else if,elsecontrol
if () {
//
} else if () {
//
} else {
//
}
while,do whileloop
while (true) {
// an infinite loop
}
// �do while loop
var input;
do {
input = get_input();
} while (isValid(input));
forloop.
for (var i=0; i < 5; i++) {
// traditional for loop control
}
// for ... of , Array
for (let value of array) {
// for of Array loop
}
// for ... in , property in Object loop
for (let property in object) {
// for in, property in object loop
}
&&and||是短路逻辑 (short-circuit),即第二个值依赖第一个结果。
var name = o && o.getName();
var name = cachedName || (cachedName = getName());
a ? 'yes': 'no'operator
var allowed = (age > 18) ? 'yes' : 'no';
switch case, use for multi branch base onnumberorstring
switch (action) {
case 'draw':
drawIt();
break;
case 'eat':
eatIt();
break;
default:
doNothing();
}
no break statement, execute will "fall through" to next level, it's better add comments to "fall through" if you really want it.
switch (a) {
case 1: // fall through
case 2:
eatIt();
break;
default:
doNothing();
}
- javascript Object can be thought of as simple collections of name-value pairs, which similar to:
- Dictionaries in Python
- Hashes in Perl and Ruby
- Hash table in C and C++
- HashMap in Java
- Associative arrays in PHP
- Everything in javascript is object. javascript program involves a great deal of hash table look up. This good they are so fast!
- two type create empty object:
// type 1
var obj = new Object();
// type 2
var obj = {};