# 35赋值技巧

``````x += 23; // x = x + 23;
y -= 15; // y = y - 15;
z *= 10; // z = z * 10;
k /= 7; // k = k / 7;
p %= 3; // p = p % 3;
d **= 2; // d = d ** 2;
m >>= 2; // m = m >> 2;
n <<= 2; // n = n << 2;
n ++; // n = n + 1;
n --; n = n - 1;

``````

### `++` 与 `--` 操作符

``````var a = 2;
var b = a++;
// 现在 a == 3  b == 2
``````

`a++`做了如下工作： 1. 返回`a`的值 2. `a`增加1

``````var a = 2;
var b = ++a;
// 现在a和b都是3
``````

`--`操作符除了使值减小外，其他功能是类似的。

### If-else (使用三元运算符)

``````var newValue;
if(value > 10)
newValue = 5;
else
newValue = 2;
``````

``````var newValue = (value > 10) ? 5 : 2;
``````

### 检测Null、Undefined、空

``````if (variable1 !== null || variable1 !== undefined || variable1 !== '') {
var variable2 = variable1;
}
``````

``````var variable2 = variable1  || '';
``````

P.S.：如果`variable1`是一个数字，则先检查他是否为0。

### 对象数组表示法

``````var a = new Array();
a[0] = "myString1";
a[1] = "myString2";
``````

``````var a = ["myString1", "myString2"];
``````

### 关联数组

``````var skillSet = new Array();
skillSet['Document language'] = 'HTML5';
skillSet['Styling language'] = 'CSS3';
``````

``````var skillSet = {
'Document language' : 'HTML5',
'Styling language' : 'CSS3'
};
``````