JavaScript: Types & Grammar
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:
- The first character must be a letter, an underscore
_, or a dollar sign$; - All other characters may be letters, underscores, dollar signs, or numbers;
- Keywords, reserved words,
true,false, andnullcannot 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
varoperator to define a variable makes it local to the scope in which it was defined. For example, defining a variable inside of a function usingvarmeans that the variable is destroyed as soon as the function exits, as shown here:
|
|

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).
|
|
The Undefined Type & The Null Type
Similarities
They are all primitive types
undefined 类型只有一个值,即特殊的undefined; null 类型只有一个值,即特殊的nullnull 和 undefined 的布尔类型都为 “false”
它们调用Boolean()后都会返回false
在if()语句中,由于 ECMAScript 会自动调用Boolean(),因此在if语句中它们的判断结果都为false
Differences
12345678910111213 alert(null===undefined); //falsevar banana;alert(typeof banana); //undefinedalert(typeof apple); //undefinedvar cherry=undefined;alert(cherry === undefined); //truealert(cherry); //undefinedalert(banana); //undefinedalert(apple);
对未初始化的变量
banana和未声明的变量apple执行typeof操作符都返回了undefined值。
但未声明的变量 apple 在执行其他操作时,则会报错。说明初始化为 undefined 值的变量、未初始化的变量 和 未声明的变量还是不一样的。
null表示 一个被赋值为表示 没有值 的值 ,即该处不应该有值。典型用法是:
- 作为函数的参数,表示该函数的参数不是对象。
- 作为对象原型链的终点。
1 Object.getPrototypeOf(Object.prototype); // null
undefined表示表示一个变量 没有被声明,或者被声明了但没有被赋值 。典型用法是:
- 变量被声明了,但没有赋值时,就等于
undefined。- 调用函数时,应该提供的参数没有提供,该参数等于
undefined。- 对象没有赋值的属性,该属性的值为
undefined。- 函数没有返回值时,默认返回
undefined。
1234567891011 var i;console.log(i); //undefinedfunction f(x) { console.log(x); };f(); //undefinedvar myObject=new Object();console.log(myObject.myProperty); //undefinedfunction myFunction(){};console.log(myFunction()); //undefined
判断一个变量是否是 undefined 或者 null
typeof variable === “undefined”
variable === null
使用等号比较时它们相等,但恒等号比较时不相等
Boolean 类型
Boolean 类型的字面值 true 和 false 是区分大小写的。
可以对任何数据类型的值调用 Boolean() 函数,而且总会返回一个 Boolean 值。
Boolean() 转换为 true 的值
| 数据类型 | 值 |
|---|---|
| Boolean | true |
| String | 任何非空字符串 |
| Number | 任何非零数字值(包括无穷大) |
| Object | 任何对象 |
| Undefined | - |
|
|
Boolean() 转换为 false 的值
| 数据类型 | 值 |
|---|---|
| Boolean | false |
| String | “” (空字符串) |
| Number | 0 和 NaN |
| Object | null |
| Undefined | undefined |
|
|
实际上,我们只需要记住 falsy 的几个值就可以了: false、空字符串、0 和 NaN、null、undefined。
Number 类型
由于保存浮点数值需要的内存空间是保存整数数值的两倍,因此 ECMAScript 会不失时机地将浮点数值转换为整数值。
浮点数值计算会产生舍入误差的问题:
正数除以 0 返回 Infinity,负数除以 0 返回 -Infinity
NaN (Not a Number)
可能出现 NaN 的情况:
|
|
NaN 的两个特点:
- 任何涉及
NaN的操作都会返回NaN。 NaN与任何值都不相等,包括NaN本身。1alert(NaN==NaN); //false
转换为数值的三种方式比较
Number()可以用于任何数据类型parseInt()是专门用于把字符串转换成数值,由于多数情况下,我们要解析的都是十进制数值,在使用parseInt()的时候最好带上第二个参数 10 。1parseInt("56.78.90abc", 10); //56parseFloat()也是专门用于把字符串转换成数值,由于parseFloat()只解析十进制数值,因此它没有第二个参数。1parseFloat("56.78.90abc"); //56.78
String 类型
如果字符串中包含转义字符,那么该字符串的 length 属性是转义后的字符数字。
在 ECMAScript 中的字符串的特点是,字符串一旦创建,它们的值就不能变。要改变某个变量保存的字符串,首先要销毁原来的字符串,然后再用另一个包含新值的字符串填充该变量。
转换为字符串的方法:
toString()
数值、布尔值、对象和字符串值都有toString()方法,但null和undefined值没有这个方法。String()
String()这个转型函数能够将任何类型的值转换为字符串。String()函数遵循下列转换规则:- 如果值有
toString()方法,则调用该方法并返回相应的结果; - 如果值是
null,则返回 “null”; - 如果值是
undefined,则返回 “undefined”;
- 如果值有
+""
使用+""的方法跟String()函数的结果相同。1234567891011121314151617181920212223242526272829303132var 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 nullString(d); //"null"var e=undefined;e.toString(); //Uncaught TypeError: Cannot read property 'toString' of undefinedString(e); //"undefined"
操作符
三元操作符: ? :
语句
switch 语句
- switch 语句在比较值时使用的是全等操作符(===),因此不会发生类型转换。
- switch 语句合并 case 的写法: 123456789101112switch (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的区别
