JavascriptSupported5 Basic Data Types:
| Number |
Numeric type |
| String |
String |
| Boolean |
True/false |
| Null |
Null
|
| Undefined |
Undefined |
In addition to these basic data types, JavaScript also supports Composite data types-objects. An object represents a set of values. There are two types of objects in javascript: an unordered set of named values (generallyObject), The other indicates an ordered set of numbered values (Array). Although in essence, objects and arrays in JavaScript are of the same data type, their behavior is extremely different, so they are usually considered as two different types.
Javascript also defines another special object-function.FunctionIs an object with executable code. You can call a function to perform some operations. Like arrays, functions behave differently from other types of objects. Javascript defines special syntax for functions. Therefore, we regard functions as data types independent of objects and arrays.
In addition to functions and arrays, the core JavaScript language also defines other specialized objects. These objects represent not new data types, but new classes of objects. The date class defines objects that represent dates, and the Regexp class defines objects that represent regular expressions... and so on.
1. Number:
Numbers are the most basic data types, and they almost do not need to be explained. The difference between JavaScript and other programming languages (such as C and Java) is that it does not distinguish between integer and floating point values. In JavaScript, all numbers are represented by float. When a number appears directly in a javascript program, we call itDirect numeric value. Javascript uses the arithmetic operators provided by the language to perform numeric operations. In addition, it also uses a large number of arithmetic functions to support more complex arithmetic operations, these functions are part of the language's core. For convenience, these functions are saved as attributes of the math object, so we always use a direct number of math to access these functions.
Javascript also uses some special values. When a floating point value is greater than the maximum value, the result is a special infinity value, which is output by JavaScriptInfinity. Similarly, when a negative value is smaller than the minimum negative value, the result is negative infinity, and the output is-Infinity.
When an arithmetic operation (such as dividing 0 by 0) produces undefined results or errors, another special JavaScript value is returned. In this case, the result is a special non-numeric value and the output isNan. The behavior of this non-numeric value is somewhat unusual because it is not equal to any value, including itself, so a special function is required.Isnan ()To detect this value. Related functionsIsfinite ()Used to check whether a number is Nan, positive infinity, or negative infinity.
Constants of special values
| Constant |
Description |
| Infinity |
Infinity |
| Nan |
Non-numeric |
| Number. max_value |
The maximum number that can be expressed. |
| Number. min_value |
The smallest number that can be expressed. |
| Number. Nan |
Non-numeric |
| Number. positive_infinity |
Positive infinity |
| Number. negative_infinity |
Negative infinity |
2. String:
A string is used to represent the data type of text. The amount of strings in a program is contained in single quotes or double quotes. Note that there is no character data type such as char in JavaScript.
Direct string count:
Strings must be directly written in one row. If they are placed in two rows, they may be truncated. If you must add a linefeed to the string directly, you can use the Character Sequence \ n. For example, "this string \ nhas two lines ". Note: When using single quotes to define strings, you must pay attention to abbreviations and all spaces in English, such as can't and O 'Reilly's. Because the apostrophes are the same as single quotes, you must use the backslash (\) to escape the apostrophes in strings with single quotes.
Escape Sequence in the direct string quantity:
In JavaScript strings, backslash (\) has a special purpose. Add a character after the backslash to indicate the characters that cannot appear in the string. For example, \ n is an escape sequence, which represents a line break.
Javascript escape sequence
| Sequence |
Characters |
| \ 0 |
Null |
| \ B |
Escape Character |
| \ T |
Horizontal Tab |
| \ N |
Line Break |
| \ V |
Vertical Tab |
| \ F |
Page feed |
| \ R |
Carriage Return |
| \" |
Double quotation marks |
| \' |
Single quotation mark or marker |
| \\ |
Backslash |
| \ Xxx |
Latin-1 character specified by two hexadecimal values xx |
| \ Uxxxx |
Unicode characters specified by four-digit hexadecimal number XXXX |
| \ Xxx |
Ecmascript V3 does not support this escape sequence. |
Use of strings:
One of the internal features of JavaScript is the ability to connect strings. If the plus sign (+) operator is used for a number, it adds two numbers. But if it is applied to the string, it will connect the two strings and append the second string after the first one. To determine the length of a string (the number of characters it contains), you can use the Length attribute of the string. To obtain the last character of string s:
last_char=s.charAt(s.length-1);
Find the location of the first letter "A" in string s:
i=s.indexOf('a');
There are many other methods to operate on strings. I will not repeat them here.
Converts a number to a string.:
There are three common methods to convert numbers into strings:
① To convert a number into a string, just add an empty string to it:
var n_as_string=n+" ";
② To explicitly convert a number to a string, you can use the string () function:
var string_value=String(number);
③ Use the tostring () method:
var string_value=number.toString( );
The tostring () method of the number object has an optional parameter, which is used to specify the conversion base. If this parameter is not specified, the conversion is performed based on 10 by default.
var n=17;binary_string=n.toString(2); //"10001"octal_string="0"+n.toString(8); //"021"hex_string="0x"+n.toString(16); //"0x11"
One disadvantage of a version earlier than JavaScript 1.5 is that there is no built-in method to convert a number into a string and specify the decimal point contained in it, or whether exponential notation should be used. This makes it difficult to display numbers in traditional formats, such as numbers that represent currency values.
Ecmascript V3 and JavaScript 1.5 solve this problem by adding three new methods for the number class.
① Tofixed () method converts a number to a string and displays the specified number of digits after the decimal point. It does not use exponential notation.
var n=123456.789;n.toFixed(0); //"123457"n.toFixed(2); //"123456.79"
② Toexponential () converts a number into a string using exponential notation. the first digit of the decimal point is one digit, and the second year has a specified number of digits:
var n=123456.789;n.toExponential(1); //"1.2e+5"n.toExponential(3); //"1.235e+5"
③ Toprecision () uses a specified meaningful number (valid number) to display a number. If the number is not enough to show the entire integer part of the number, it uses exponential notation.
var n=123456.789;n.toPrecision(4); //"1.235e+5"n.toPrecision(7); //"123456.8"
Converts a string to a number.:
There are three common methods to convert a string to a number:
① When a string is used in a numeric environment, it is automatically converted to a number. Therefore, a string minus 0 can be converted into a number.
var number=string_value-0;
② Call the number () constructor
var number=Number(string_value);
③ Call parseint () or parsefloat ()
parseInt("3 blind mice"); //3parseFloat("3.14 meters"); //3.14
3. Boolean Value:
Boolean data has only two values: true and false. In JavaScript, Boolean values are easy to convert and can be easily converted from other types to boolean values, and are often automatically converted.
①If a Boolean value is used in a numeric environment, true is converted to number 1, and false is converted to number 0.
②If a Boolean value is used in a string, true is converted to "true", and false is converted to "flase ".
③If a number is used in a place that is supposed to be a Boolean value, if the number is 0 or Nan, it is converted to false; otherwise, it is converted to true.
④If a string is used where a Boolean value is supposed to be used, the empty string is converted to false. Otherwise, the string is converted to true.
⑤Null or undefined is also converted to false, and any non-empty object, array, or function is converted to true.
If you prefer to convert a type to an explicit one, you can use the Boolean () function: var x_as_boolean = Boolean (X );
4. null:
Null indicates "null ". If the value of a variable is null, it indicates that its value is not a valid object, array, number, string, or Boolean value.
① When null is used in a Boolean environment, it is converted to false.
② When null is used in a digital environment, it is converted to 0.
③ When null is used in a string environment, it is converted to "null ".
5. undefined:
When a variable that has not been declared is used, or a variable that has been declared but has not been assigned a value, or an object attribute that does not exist, undefined is returned.
① When undefined is used in a Boolean environment, it is converted to false.
② When undefined is used in a digital environment, it is converted to Nan.
③ When undefined is used in a string environment, it is converted to "undefined ".