值(Values)

由于页面重复,请您不要再翻译该页面

同名页面为Core JavaScript 1.5 Guide:Values

Values

Javascript识别以下的值类型:

  • Numbers, 如 42 或3.14159
  • Logical (Boolean) values, 可以为true 或者 false
  • Strings, 比如 "Howdy!"
  • null, 一个特殊的空字符; null 也是一个初始值. 因为Javascript区分大小写, null 不同于Null, NULL, 或者其他的变量
  • undefined, 值没有被定义的高级属性; undefined 也是一个初始值.

This relatively small set of types of values, or data types, enables you to perform useful functions with your applications. There is no explicit distinction between integer and real-valued numbers. Nor is there an explicit date data type in JavaScript. However, you can use the Date object and its methods to handle dates. Objects and functions are the other fundamental elements in the language. You can think of objects as named containers for values, and functions as procedures that your application can perform.

类型转换

JavaScript 是一个弱类型的语言。这意味着你在声明变量的时候不需要指定数据类型,程序在执行的过程中数据的类型会根据需要自动的转换。例如:你先声明了下面这个变量:

var answer = 42

And later过了一会儿,你需要改变重新赋值成一个字符串:

answer = "Thanks for all the fish..."

JavaScript 解析器会自动将类型进行转换,而不会产生错误。

在表达式中进行数字与字符串的“+”操作时,JavaScript会将数字转换成字符串。例如:

x = "The answer is " + 42 // returns "The answer is 42"
y = 42 + " is the answer" // returns "42 is the answer"

在其他运算符的情况下,JavaScript 不将数字转换成字符串。例如:

"37" - 7 // returns 30
"37" + 7 // returns "377"

Document Tags and Contributors

Contributors to this page: Oatn, Cnmahj
最后编辑者: Cnmahj,