Case-sensitivity

The Format of the Identifiers:

An identifier is the name of a variable, function, property, or function argument. Identifiers may be
one or more characters in the following format:

  1. The first character must be a letter, an underscore _ , or a dollar sign $ ;
  2. All other characters may be letters, underscores, dollar signs, or numbers;
  3. Keywords, reserved words, true, false, and null cannot be used as identifiers.

By convention, ECMAScript identifiers use camel case, like this: myFirstCar.

Variables

  • Without initialization, the variable will holds the special value undefined.
  • It’s important to note that using the var operator to define a variable makes it local to the scope in which it was defined. For example, defining a variable inside of a function using var means that the variable is destroyed as soon as the function exits, as shown here:
1
2
3
4
5
function test(){
var num=1; //local variable
}
test();
alert(num);

Data Types

There are five simple data types (also called primitive types) in ECMAScript: Undefi ned, Null,
Boolean, Number, and String. There is also one complex data type called Object, which is an
unordered list of name-value pairs.

The typeof Operator

Because typeof is an operator and not a function, no parentheses are required (although they can be used).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
console.log(typeof 2015); // number
var a, b=true, c=2015, d='2015';
console.log('The data type of "a": '+typeof a); // The data type of "a": undefined
console.log('The data type of "b": '+typeof b); // The data type of "b": boolean
console.log('The data type of "c": '+typeof c); // The data type of "c": number
console.log('The data type of "d": '+typeof d); // The data type of "d": string
console.log('The data type of "NaN": '+typeof NaN); // The data type of "NaN": number
console.log('The data type of "null": '+typeof null); // The data type of "null": object
console.log('The data type of "[1,2]": '+typeof [1,2]); // The data type of "[1,2]": object
var myDate=new Date();
console.log('The data type of "myDate": '+typeof myDate); // The data type of "myDate": object
var myRegExp=/"(?:\\.|[^\\\"])*"/g;
console.log('The data type of "myRegExp": '+typeof myRegExp); // The data type of "myRegExp": object

The Undefined Type & The Null Type

Similarities

  1. They are all primitive types
    undefined 类型只有一个值,即特殊的 undefined ; null 类型只有一个值,即特殊的 null

  2. null 和 undefined 的布尔类型都为 “false”
    它们调用 Boolean() 后都会返回 false
    if() 语句中,由于 ECMAScript 会自动调用 Boolean() ,因此在if语句中它们的判断结果都为 false


Differences

1
2
3
4
5
6
7
8
9
10
11
12
13
alert(null===undefined); //false
var banana;
alert(typeof banana); //undefined
alert(typeof apple); //undefined
var cherry=undefined;
alert(cherry === undefined); //true
alert(cherry); //undefined
alert(banana); //undefined
alert(apple);

对未初始化的变量 banana 和未声明的变量 apple 执行 typeof 操作符都返回了 undefined 值。
但未声明的变量 apple 在执行其他操作时,则会报错。说明初始化为 undefined 值的变量、未初始化的变量 和 未声明的变量还是不一样的。

null 表示 一个被赋值为表示 没有值 的值 ,即该处不应该有值。典型用法是:

  1. 作为函数的参数,表示该函数的参数不是对象。
  2. 作为对象原型链的终点。
    1
    Object.getPrototypeOf(Object.prototype); // null

undefined 表示表示一个变量 没有被声明,或者被声明了但没有被赋值 。典型用法是:

  1. 变量被声明了,但没有赋值时,就等于 undefined
  2. 调用函数时,应该提供的参数没有提供,该参数等于 undefined
  3. 对象没有赋值的属性,该属性的值为 undefined
  4. 函数没有返回值时,默认返回 undefined
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    var i;
    console.log(i); //undefined
    function f(x) { console.log(x); };
    f(); //undefined
    var myObject=new Object();
    console.log(myObject.myProperty); //undefined
    function myFunction(){};
    console.log(myFunction()); //undefined

判断一个变量是否是 undefined 或者 null

typeof variable === “undefined”

1
typeof undefined === "undefined"; //true

variable === null

1
null === null //true

使用等号比较时它们相等,但恒等号比较时不相等

1
2
null == undefined // true
null === undefined // false

Boolean 类型

Boolean 类型的字面值 true 和 false 是区分大小写的。
可以对任何数据类型的值调用 Boolean() 函数,而且总会返回一个 Boolean 值。

Boolean() 转换为 true 的值

数据类型
Boolean true
String 任何非空字符串
Number 任何非零数字值(包括无穷大)
Object 任何对象
Undefined -
1
2
3
Boolean('false'); //true
Boolean('0'); //true
Boolean([]); //true

Boolean() 转换为 false 的值

数据类型
Boolean false
String “” (空字符串)
Number 0 和 NaN
Object null
Undefined undefined
1
2
3
4
5
6
Boolean(false); //false
Boolean(''); //false
Boolean(0); //false
Boolean(NaN); //false
Boolean(null); //false
Boolean(undefined); //false

实际上,我们只需要记住 falsy 的几个值就可以了: false、空字符串、0 和 NaNnullundefined

Number 类型

由于保存浮点数值需要的内存空间是保存整数数值的两倍,因此 ECMAScript 会不失时机地将浮点数值转换为整数值。

浮点数值计算会产生舍入误差的问题:

1
0.1+0.5+0.07; //0.6699999999999999

正数除以 0 返回 Infinity,负数除以 0 返回 -Infinity

1
2
1/0; //Infinity
-1/0; //-Infinity

NaN (Not a Number)

可能出现 NaN 的情况:
1
2
3
4
5
6
7
8
9
10
11
0/0; //NaN
Number(undefined); //NaN
Number("Hello World!"); //NaN
parseInt(""); //NaN
parseInt("ABC"); //NaN
var t="abc"
t=+t; //NaN
Infinity-Infinity; //NaN
NaN-NaN; //NaN 任何涉及 NaN 的操作都会返回 NaN
NaN 的两个特点:
  1. 任何涉及 NaN 的操作都会返回 NaN
  2. NaN 与任何值都不相等,包括 NaN 本身。
    1
    alert(NaN==NaN); //false
转换为数值的三种方式比较
  1. Number() 可以用于任何数据类型
  2. parseInt() 是专门用于把字符串转换成数值,由于多数情况下,我们要解析的都是十进制数值,在使用 parseInt() 的时候最好带上第二个参数 10 。

    1
    parseInt("56.78.90abc", 10); //56
  3. parseFloat() 也是专门用于把字符串转换成数值,由于 parseFloat() 只解析十进制数值,因此它没有第二个参数。

    1
    parseFloat("56.78.90abc"); //56.78

String 类型

如果字符串中包含转义字符,那么该字符串的 length 属性是转义后的字符数字。

1
2
alert("\u03a3"); //Σ
alert("\u03a3".length); //1

在 ECMAScript 中的字符串的特点是,字符串一旦创建,它们的值就不能变。要改变某个变量保存的字符串,首先要销毁原来的字符串,然后再用另一个包含新值的字符串填充该变量。

转换为字符串的方法:

  1. toString()
    数值、布尔值、对象和字符串值都有 toString() 方法,但 nullundefined 值没有这个方法。
  2. String()
    String() 这个转型函数能够将任何类型的值转换为字符串。String() 函数遵循下列转换规则:
    • 如果值有 toString() 方法,则调用该方法并返回相应的结果;
    • 如果值是 null,则返回 “null”;
    • 如果值是 undefined,则返回 “undefined”;
  3. +""
    使用 +"" 的方法跟 String() 函数的结果相同。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    var a=11;
    a.toString(); //"11"
    var b=true;
    b.toString(); //"true"
    var c=[1,["a",[2,3]]];
    c.toString(); //"1,a,2,3"
    var myDate=new Date();
    myDate.toString(); //"Sat Oct 10 2015 01:03:39 GMT+0800 (中国标准时间)"
    var myObject={
    name:"ABC",
    time:"2days"
    };
    myObject.toString(); //"[object Object]"
    var myRegExp=/"(?:\\.|[^\\\"])*"/g;
    myRegExp.toString(); //"/"(?:\\.|[^\\\"])*"/g"
    var myString="abc";
    myString.toString(); //"abc"
    var d=null;
    d.toString(); //Uncaught TypeError: Cannot read property 'toString' of null
    String(d); //"null"
    var e=undefined;
    e.toString(); //Uncaught TypeError: Cannot read property 'toString' of undefined
    String(e); //"undefined"

操作符

三元操作符: ? :

1
变量=布尔值 ? true_value : false_value

语句

switch 语句

  1. switch 语句在比较值时使用的是全等操作符(===),因此不会发生类型转换。
  2. switch 语句合并 case 的写法:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    switch (i) {
    case "a":
    /*合并两种情况*/
    case "b":
    alert("a or b");
    break;
    case 1:
    alert("1");
    break;
    default:
    alert("Other");
    }

References:

Nicholas C. Zakas, JavaScript高级程序设计(第3版): 人民邮电出版社 , 2012-3-29 , 第三章
Joel Lovera, jstips, 05 Jan 2016, Differences between undefined and null
阮一峰, 阮一峰的网络日志, 2014年3月28日, undefined与null的区别

Content

  1. Case-sensitivity
  2. The Format of the Identifiers:
  3. Variables
  4. Data Types
    1. The typeof Operator
    2. The Undefined Type & The Null Type
      1. Similarities
      2. Differences
      3. 判断一个变量是否是 undefined 或者 null
    3. Boolean 类型
    4. Number 类型
      1. NaN (Not a Number)
        1. 可能出现 NaN 的情况:
        2. NaN 的两个特点:
        3. 转换为数值的三种方式比较
    5. String 类型
    6. 操作符
    7. 语句
      1. switch 语句
  5. References: