Home > Article > Web Front-end > What does Javascript data type not include?
The data types of Javascript do not include Symbol. JavaScript has 6 data types, namely Undefined, Null, Boolean, Number, String and Object.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
What does Javascript’s data type not include?
##This article is based on (Advanced Programming with JavaScript (3rd Edition)) Summary
The six major data types of JavaScript (excluding Symbol)
Undefined,
Null,
Boolean,
Number,
String and
Object
are 7 in ES6 There is an additional data type Symbol, which is not covered in this article.
Undefined means undefined,
Null means empty,
Boolean represents a Boolean value,
Number represents a numerical value, and String represents a string.
Usually we can use the
typeof operator to detect the data type of a variable.
Note that typeof is an operator! Operator! Operator!The typeof operator applies the following rules:
- This value is undefined.
——This value is a Boolean value
——This value is a string
——This value is a numerical value
——This value is an object or Null
——This value is the function
str="I am String type":
<p style="line-height: normal;">console.log(typeof str) //"string" 注意typeof操作符的返回的结果是字符串<br/></p>For the typeof operator, there are A little weird thing is:
1. typeof returns
"object" for
null,
2. typeof returns
"undefined for undeclared variables "
3. typeof also returns
"undefined"
var s = null console.log(typeof s) //"object" console.log(typeof a) //"undefined", 注意变量a未声明 var b; console.log(typeof b) //"undefined", 注意变量b声明但未初始化for variables that are declared but not initialized, because of the weird characteristics of
typeof, and we also To learn more about the specific information of the variable, we often use instanceof to determine the data type of the variable.
Undefined and
Null types have only one value, which are
undefined and
null respectively .
For
undefined, you need to remember the following two points:
(refer to the above code )
is equal to
null
console.log(undefined == null) //true
null, you need to remember two points:
Returns
"object". Logically, null is a reference to object (for examples, see
typeof)
is equal to
null
Boolean type has two values - —
true and
false.
Any data can call the Boolean function
Boolean(), which will return a Boolean value.
true | false | |
---|---|---|
true | false | |
Non-zero string | ""(empty string) | |
Non-zero numbers (including infinity) | 0 and NaN | |
Any object | null | |
Not applicable | undefined | |
Not applicable | null |
字面量 | 含义 |
---|---|
\n | 换行符 |
\t | 制表符 |
\b | 退格符 |
\r | 回车符 |
\f | 换页符 |
\ | 斜杠 |
\’ | 单引号 |
\” | 双引号 |
\xnn | x表示十六进制, n为0-F, nn表示一个字符. 如\x41表示”A” |
\unnnn | u表示Unicode, 也为十六进制. nnnn表示一个十六进制的Unicode字符 |
例如:
console.log("这是单引号: \'") //这是单引号: 'console.log("这是\n换行符")/* 这是 换行符 */console.log("这是大写字母: \x41") //这是大写字母: A
大部分值都有toString()方法, 因此我们可以使用这个方法.
var a = 2console.log(a.toString()) //2var b = trueconsole.log(b.toString()) //true
还可以给toString()
添加一个参数, 这个参数表示基数.
var num = 7console.log(num.toString(2)) //111console.log(num.toString(3)) //21
前面说了大部分值可以使用toString()
方法, 那么哪些值不能使用呢? 那就是null
和undefined
.
当我们需要将一个变量A转换为字符串时, 假如我们不知道变量A是否是null
和undefined
, 我们可以使用String()
函数. 这个函数可以讲任意类型的值转换为字符串. 其规则如下:
toString()
方法则调用该方法.null
, 则返回"null"
undefined
, 则返回"undefined"
Object
类型俗称对象, 对象的实例通常使用new
操作符进行创建. 对象的实例还是对象, 我们会在对象的实例中添加属性和方法.
var obj = new Object();
Object
的实例有下列基本的属性和方法:
constructor
constructor
属性保存着穿件当前对象的函数, 也叫构造函数. 如上例中的Object()
hasOwnProperty(propertyName)
propertyName
的属性. propertyName
必须为字符串isPrototypeOf(object)
propertyIsEnumerable(propertyName)
propertyName
是否可以用for-in
语句来枚举. propertyName
必须为字符串toLocaleString()
, toString()
, valueOf()
valueOf()
还可以返回对象的数值, 布尔值表示. var obj = new Object() obj.constructor //ƒ Object() { [native code] }obj.name = "ES" //给obj添加属性obj.hasOwnProperty("name") //true, 注意参数必须为字符串形式obj.propertyIsEnumerable("name") //trueobj.toString() //"[object Object]"
推荐学习:《javascript高级教程》
The above is the detailed content of What does Javascript data type not include?. For more information, please follow other related articles on the PHP Chinese website!