Home > Web Front-end > JS Tutorial > body text

Summary of JavaScript types, values ​​and variables_javascript skills

WBOY
Release: 2016-05-16 15:50:57
Original
1036 people have browsed it

Foreword: JavaScript data types are divided into two categories: primitive types and object types. 5 primitive types: number, string, Boolean value, null (empty), undefined (undefined). An object is a collection of attributes, and each attribute is composed of a "name/value pair" (the value can be a primitive value or an object). Three special objects: global objects, arrays, and functions. The core of the JavaScript language also defines three useful classes: date (Date) class, regular (RegExp) class, and error (Error) class.

 1 Number

JavaScript does not distinguish between integer values ​​and floating point values. JavaScript can recognize decimal integer literals (the so-called literals are data values ​​directly used in the program), and hexadecimal values ​​(prefixed with 0x or 0X, which is the number 0 and not the letter o. Think about it if it is the letter o If so, then the hexadecimal value becomes an identifier.) Although the ECMAScript standard does not support octal literals, some implementations of JavaScript can use octal form to represent integers (prefixed with the number 0). The author uses octal to represent an integer in the three browsers of IE, Chrome, and FF on my computer. Variable assignment is no problem either. However, in the strict mode of ECMAScript6, octal literals are explicitly prohibited.

There are two ways to write floating point literals. ① Traditional way of writing real numbers: It consists of an integer part, a decimal point and a decimal part; ② Exponent counting method: that is, the real number is followed by the letter e or E, followed by a positive or negative sign, and then followed by an integer exponent.

 1.1 Overflow of arithmetic operations

Arithmetic operations in JavaScript will not report errors when overflowing, underflowing, or being divisible by 0.

Overflow: When the operation result exceeds the upper limit of the number that JavaScript can represent, the result is positive infinity or negative infinity-Infinity. The behavioral characteristics of infinity values ​​are also consistent with reality: the results of addition, subtraction, multiplication and division operations based on them are still infinite values ​​(of course retaining their signs); underflow: when the operation result is infinitely close to zero and is smaller than the smallest value that JavaScript can represent What happened when the value was still small. In this case, 0 will be returned. The special value "negative zero" is returned when a negative number underflows. Negative zero and whole zero are essentially equal (can even be tested using strict equality ===), except as divisors:

var zero = 0;  //正零值
var negz = -0;  //负零值
zero === negz  //表达式返回值为true
1/zero === 1/negz  
//表达式返回值false,等价于判断正无穷大和负无穷大是否严格相等

Copy after login

Divisibility by 0 will return positive infinity or negative infinity. But dividing 0 by 0 will return NaN (the value of the NaN property of the JavaScript predefined object Number). There are four situations where NaN is returned: ① 0 divided by 0 ② infinity divided by infinity ③ performing a square root operation on any negative number ④ when arithmetic operators are used with operands that are not numbers or cannot be converted to numbers.

There is something special about the NaN value: it is not equal to any value, including itself. There are two ways to judge whether a variable x is NaN: ① Use the function isNaN() ② Use x != x to judge. The expression result is true if and only if x is NaN. There is a similar function isFinite() in JavaScript, which returns true when the parameter is not NaN, Infinity or -Infinity.

 1.2 Binary floating point numbers and rounding errors

There are countless real numbers, but JavaScript can only represent a limited number of them in the form of floating point numbers. In other words, when using real numbers in JavaScript, they are often just an approximate representation of a real value. JavaScript uses IEEE-754 floating point number representation, which is a binary representation that can accurately represent fractions such as 1/2, 1/8, and 1/1024, but decimal fractions 1/10, 1/10 etc. cannot be expressed accurately. For example:

var x = 0.3 -0.2;  //x=0.09999999999999998
var y = 0.2 - 0.1;  // y=0.1
x == y       //false
x == 0.1      //false
y == 0.1      //true
0.1 == 0.1     //true
var z = x + y;   //z=0.19999999999999998

Copy after login

  2 文本

  2.1 字符串、字符集

  字符串(string)是一组由16位值组成的不可变的有序序列,每个字符通常来自于Unicode字符集。字符串的长度(length)是其所含16位值得个数。JavaScript通过字符串类型来表示文本。注意:JavaScript中并没有表示单个字符的“字符型”。要表示一个16位值,只需将其赋值给字符串变量即可。

  JavaScript采用UTF-16编码的Unicode字符集,JavaScript字符串是由一组无符号的16位值组成的序列。那些不能表示为16位的Unicode字符则遵循UTF-16编码规则——用两个16位值组成一个序列(或称作“代理项对”)表示。这意味着一个长度为2的JavaScript字符串有可能表示一个Unicode字符。注意:JavaScript定义的各式字符串的操作方法均作用于16位值,而非字符,且不会对代理项对做单独处理。书看到这里,又结合http://www.alloyteam.com/2013/12/js-calculate-the-number-of-bytes-occupied-by-a-string/上面所述,终于对Unicode字符集、UTF-8、UTF-16稍有理解。

  字符串的定界符可以是单引号或者双引号。这两种形式的定界符可以嵌套,但是不能多层嵌套(比如,双引号可以包含单引号,这时单引号中不能再包含双引号了)。正如上篇所说,一个字符串值可以拆分为数行,每行必须以反斜线(\)结束,这时反斜线和行结束符都不算是字符串内容,即字符串本身并非是多行,只是写成了多行的形式。

  注意:①在JavaScript中字符串是固定不变的(除非重新赋值),类似replace()和toUpperCase()的方法都返回新字符串,原字符串本身并没有变化;②字符串可以当做只读数组,除了使用charAt()方法来查询一个单一字符,也可以使用方括号的方式来访问字符串中的单个字符(16位值),例如:

    s = "hello, world";
    s[0]   //=>"h"

Copy after login

 2.2 Escape character

Escape character meaning
o NUL character (u0000)
b Backspace character (u0008)
t Horizontal tab (u0009)
n newline character (u000A)
v Vertical tab (u000B)
f form feed character (u000C)
r carriage return (u000D)
" Double quotes (u0022)
' Apostrophe or single quote (u0027)
\ Backslash(u005C)
xXX Latin-1 character specified by the two-digit hexadecimal number XX
uXXXX Unicode character specified by 4-digit hexadecimal number XXXX
                                                                                                                                                                                                      Note: If the "" character precedes a character not listed in the table, the "" character is ignored. For example, "#" and "#" are equivalent. Don’t forget that backslashes also have a role in using backslashes at the end of each line in multi-line strings. 

 3 Boolean value

Values ​​in JavaScript can be converted to Boolean values. Among them, null, undefined, 0, -0, NaN, "" (empty string), these 6 values ​​will be converted to false, false and these six values ​​​​are sometimes called "false values"; all other values, including Objects (arrays) will be converted to true, and true and these values ​​are called "true values". Note: Boolean contains the toString() method, so you can use this method to convert a string to "true" or "false", but it does not contain other useful methods.

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template