Home>Article>Web Front-end> JavaScript basic data types you must know
This article brings you relevant knowledge about the basic data types in JavaScript. I hope it will be helpful to you.
JavaScriptis a weakly typed scripting language. There is no need to specify the data type of the variable when declaring it.JavaScriptThe data type of the variable is dynamically determined during interpretation. But the value ofJavaScriptis stored in memory and is also a data type.JavaScriptThere are five basic data types as follows
(1)Numeric type
(2)Boolean type
(3)String type
(4)UndefinedType
(5 ) NullType
is different from strongly typed languages such asC,Javais different. The numerical type ofJavaScriptnot only includes all integer variables, but also all floating-point variables.JavaScriptValues in the language are saved inIEEE 754double-precision floating point format. The numerical format inJavaScriptis very rich and fully supports expression in scientific notation. The scientific notation format is5.12e2which represents5.12multiplied by10##2to the power,5.12E2also represents5.12times10to the2power.
In scientific notation,Eis the interval symbol, andEis not case-sensitive.
Note: Numerical direct quantities should not start with0. Because JavaScript supports octal and hexadecimal. Octal starts with 0 and hexadecimal starts with 0x or 0X.
When a numeric type exceeds its representation range, two special values will appear:Infinity(positive Infinity) and-Infinity(negative infinity.)
must be enclosed in quotation marks. The quotation marks here can be either single quotation marks or double quotation marks.
var a = "12345678912aaa"; var a = '12345678912aaa';
Note: There are two main differences between strings inJavaScriptandJava:
JavaScript以String内建类表示字符串,String类里包含了一系列方法操作字符串,String类有如下基本方法和属性操作字符串:
(1)charCodeAt() :返回字符串中特定索引处的字符所对应的Unicode值
(2)Legth():返回字符串的长度
(3)toUpperCase() : 将stringObj中的小写字母全部转成大写字母
(4)toLowerCase() :将stringObj中的大写字母全部转成小写字母
(5)fromCharCode() :直接通过String类调用方法,将一系列Unicode值转换成字符串
(6)indexOf() :返回字符串第一次出现的位置
(7)lastIndexOf() :返回字符串最后一次出现的位置
(8)subString() :截取stringObj从start开始,至end为止之前的所有字符,即包括start处的字符,但不包含end处的字符
(9)slice() :截取stringObj从start开始,到end为止之前的所有字符;即包括start处的字符,但不包括end处的字符。start与end均可为负值,当为负值时,表示从最后一个字符算起的第n个字符,比如-1表示最后一个字符,-2表示倒数第二个字符。
(10)match() :在字符串内检索指定的正则表达式的匹配结果,该结果与regexp是否有指定全局标志g有关
(11)split() :将separtor为为分隔,将stringObj分割成一个字符串数组。separator可以是字符串或者正则表达式,若为字符串,则以separator为分割符;弱separator为正则表达式,则以符合separator指定模式的字符串作为分隔符。
(12)replace() :将字符串中某个子串以特定字符串替代。
下面是一些常用的转义字符
序 |
转义字符 |
使用说明 |
---|---|---|
0 |
NUL 字符(\u0000) |
|
1 |
\b |
后退一格(Backspace)退格符(\u0008) |
2 |
\f |
换页(Form Feed)(\u000C) |
3 |
\n |
换行(New Line)(\u000A) |
4 |
\r |
回车(Carriage Return)(\u000D) |
5 |
\t |
Tab Horizontal Tab (\u0009) |
6 |
\' |
Single quotation marks (\u0027) |
7 |
\" |
##Double quotes (\u0022) |
8 | \\ | ##Backslash(\u005C)|
##\v | ##Vertical tab (\u000B) |
10 |
\xNN |
consists of two hexadecimal values NN Specified Latin-1 characters |
##11 |
Unicode character specified by four hexadecimal numbers | NNNN | 12 |
From one to three Latin-1 character specified as an octal number (1 to 377). | ECMAScript v3 is not supported, do not use this escape sequence | 3.布尔类型布尔类型的值只有两个:true和false。布尔类型的值通常是逻辑运算的结果,或用于标志对象的某种状态。 4. Undefined和nullUndefined类型的值只有一个undefined,该值用于表示某个变量不存在,或者没有为其分配值,也用于表示对象的属性不存在。null用于表示变量的值为空。Undefined与null之间的差别比较微妙,总体而言,undefined表示没有为变量设置值或属性不存在,而null表示变量是有值的,只是为其值为null。 但如果不进行精确比较,很多时候undefined和null本身就想等,即null==undefined将返回true。如果要精确区分null和undefined,应该考虑使用精确等于符(===) 5.正则表达式正则表达式的本质是一种特殊的字符串,这种特殊字符允许使用“通配符”,因此一个正则表达式字符串可以匹配一批普通字符串。 元字符 名称 匹配对象
\0 NUL字符\u0000 [\b] 匹配退格符\u0008,不要与\b混淆 \t 制表符\u0009 \n 换行符\u000A \v 垂直制表符\u000B \f 换页符\u000C \r 回车符\u000D \xnn 由十六进制数nn指定的拉丁字符 \uxxxx 由十六进制数xxxx指定的Unicode字符(\u4e00-\u9fa5代表中文) \cX 控制字符^X,表示ctrl-[X],其中的X是A-Z之中任一个英文字母,用来匹配控制字符 {n} 匹配n次 {n,m} 匹配至少n次,最多m次 {n,} 匹配至少n次 ? 相当于{0,1} * 相当于{0,} + 相当于{1,} 【相关推荐:javascript学习教程】 |
The above is the detailed content of JavaScript basic data types you must know. For more information, please follow other related articles on the PHP Chinese website!