Home >Web Front-end >Front-end Q&A >What are the basic data types of javascript?
Javascript has 5 basic data types, namely: 1. Undefined type; 2. Null type; 3. Boolean type; 4. Number type; 5. String type.
The operating environment of this article: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.
ECMAScript
: Undefined
, Null
, Boolean
, Number
and String
. There is also 1 complex data type——Object
, Object
is essentially composed of a set of unordered name-value pairs.
Among them, Undefined
, Null
, Boolean
, and Number
all belong to basic types. Object
, Array
and Function
are reference types. String
is somewhat special and will be analyzed below.
ECMAScript
uses the var
keyword to define variables, because js
is weakly typed, so it cannot Determine what value the variable will store, so you don't know what type the variable will be, and the type of the variable can change at any time.
This is why ECMAScript
is a loose type. The so-called loose type can be used to save any type of data.
ps: es6
has added the let
command to declare variables, const
The command declares a read-only constant. The usage of
let
is similar to var
, but the variables declared are only in the code where the let
command is located Valid within the block.
#const
Once declared, the value of a constant cannot be changed.
About let
, const
will not be discussed here.
Since the variables in js
are loosely typed, it provides a way to detect the data type of the current variable, and also It is the typeof keyword.
Through the typeof
keyword, the following values will be returned for the five data types (displayed in string form)undefined
--- ------- If the value is undefined Undefined
boolean
’ ’ ’ ’ ’ s ’ ’ s ‐ to Boolean ‐ ‐‐ ‐‐‐‐‐‐ ‐ Boolean
string ---------- If the value is a string
String
---------- If this value is a numeric type Number
##object
null
ObjectIt should be noted that
typeof null
object because of the special value
null is considered a null object reference. The
Undefined
undefined. When a variable is declared using
var but is not initialized, the value of the variable is
undefined. However, it is generally recommended to initialize variables as much as possible, but in early
js versions, the value
undefined was not specified, so in some frameworks, in order to be compatible with older browsers,
windowObject adds
undefined value. The
window['undefined'] = window['undefined']; //或者 window.undefined = window.undefined;
Null
null. From a logical point of view, the
null value represents a null object pointer, which is why using the
typeof operator to detect
null will return
object s reason.
var car = null; console.log(typeof car); // "object"
If the defined variable is going to be used to hold objects in the future, it is best to initialize the variable to
null rather than to another value. In this way, as long as the null value is directly detected, you can know whether the corresponding variable has saved a reference to an object.
For example: <pre class="brush:php;toolbar:false"> if(car != null){
//对car对象执行某些操作
}</pre>
In fact, the undefined value is derived from the null value, so ECMA-262 stipulates that their equality test should return true.
console.log(undefined == null); //trueAlthough null and undefined have this relationship, their uses are completely different. Under no circumstances is it necessary to explicitly set the value of a variable to undefined, but the same rule does not apply to null. In other words, as long as a variable that is intended to hold an object does not actually hold an object, you should explicitly let the variable hold a null value. Doing so not only reflects the convention of null as a null object pointer, but also helps to further distinguish null and undefined.
该类型只有两个字面值:true和false。这两个值与数字值不是一回事,因此true不一定等于1,而false也不一定等于0。
虽然Boolean类型的字面值只有两个,但JavaScript中所有类型的值都有与这两个Boolean值等价的值。要将一个值转换为其对应的Boolean值,可以调用类型转换函数Boolean(),例如:
var message = 'Hello World'; var messageAsBoolean = Boolean(message);
在这个例子中,字符串message被转换成了一个Boolean值,该值被保存在messageAsBoolean变量中。可以对任何数据类型的值调用Boolean()函数,而且总会返回一个Boolean值。至于返回的这个值是true还是false,取决于要转换值的数据类型及其实际值。下表给出了各种数据类型及其对象的转换规则。
数据类型 | 转换为true的值 | 转换为false的值 |
---|---|---|
Boolean | true | false |
String | 任何非空的字符串 | ""(空字符串) |
Number | 任何非0数值(包括无穷大) | 0和NAN |
Object | 任何对象 | null |
Undefined | 不适用 | undefined |
var message = 'Hello World'; if(message) { alert("Value is true"); }
运行这个示例,就会显示一个警告框,因为字符串message被自动转换成了对应的Boolean值(true)。由于存在这种自动执行的Boolean转换,因此确切地知道在流控制语句中使用的是什么变量至关重要。
ps:使用!!操作符转换布尔值
!!一般用来将后面的表达式强制转换为布尔类型的数据(boolean),也就是只能是true或者false;
对null与undefined等其他用隐式转换的值,用!操作符时都会产生true的结果,所以用两个感叹号的作用就在于将这些值转换为“等价”的布尔值;
var foo; alert(!foo);//undifined情况下,一个感叹号返回的是true; alert(!goo);//null情况下,一个感叹号返回的也是true; var o={flag:true}; var test=!!o.flag;//等效于var test=o.flag||false; alert(test);
这段例子,演示了在undifined和null时,用一个感叹号返回的都是true,用两个感叹号返回的就是false,所以两个感叹号的作用就在于,如果明确设置了变量的值(非null/undifined/0/”“等值),结果就会根据变量的实际值来返回,如果没有设置,结果就会返回false。
【推荐学习:javascript基础教程】
这种类型用来表示整数和浮点数值,还有一种特殊的数值,即NaN(非数值 Not a Number)。这个数值用于表示一个本来要返回数值的操作数未返回数值的情况(这样就不会抛出错误了)。例如,在其他编程语言中,任何数值除以0都会导致错误,从而停止代码执行。但在JavaScript中,任何数值除以0会返回NaN,因此不会影响其他代码的执行。
NaN本身有两个非同寻常的特点。首先,任何涉及NaN的操作(例如NaN/10)都会返回NaN,这个特点在多步计算中有可能导致问题。其次,NaN与任何值都不相等,包括NaN本身。例如,下面的代码会返回false。
alert(NaN == NaN); //false
String类型用于表示由零或多个16位Unicode字符组成的字符序列,即字符串。字符串可以由单引号(')或双引号(")表示。
String类型的特殊性
string类型有些特殊,因为字符串具有可变的大小,所以显然它不能被直接存储在具有固定大小的变量中。由于效率的原因,我们希望JS只复制对字符串的引用,而不是字符串的内容。但是另一方面,字符串在许多方面都和基本类型的表现相似,而字符串是不可变的这一事实(即没法改变一个字符串值的内容),因此可以将字符串看成行为与基本类型相似的不可变引用类型
Boolean、Number、String 这三个是Javascript中的基本包装类型,也就是这三个其实是一个构造函数,他们是Function的实例,是引用类型,至于这里的String与以上说的String是同名,是因为其实上文说的String是指字符串,这里的String指的是String这个构造函数,上面那么写,是为了更好的理解,因为Javascript是松散类型的。我们可以看下String实例化的例子:
var name = String("jwy"); alert(typeof name);//"string" var x=new String('12345') typeof x //object x='12345' typeof x //string var author = "Tom"; alert(typeof name);//"string"
至于author这个会有length,substring等等这些方法,其实string只是String的一个实例,类似于C#中的String,和string.
注意,typeof 变量 如果值是"string" 的话,也就是这个变量是字符串,在Javascript中,字符串是基本类型,而在C#或Java中,字符串是引用类型,但是Javascript中的String是引用类型,因为它是Javascript中定义好的基本包装类型,在C#中,String跟string其实是一样的。
本帖只是简要的copy了一些JavaScript高级程序设计(第三版)内容,外加了自己侧重的角度,看本帖的朋友还是要看书啊,这里只是做个参考。
The above is the detailed content of What are the basic data types of javascript?. For more information, please follow other related articles on the PHP Chinese website!