Dynamic type
JavaScript is a weak type (Dynamic language). This means that the type of the variable is not declared until it is used, and its type is automatically acknowledged when the program is run.
Data type
There are five simple data types in ECMAScript: Undefined,null,boolean,number,string. There is also a complex data type--object,object essentially consists of a set of unordered name-value pairs. ECMAScript does not support any custom type mechanism, and eventually all values will be one of the above 6 data types.
Undefined type
Its type has only one value, that is, the special undefined. When using Var to declare a variable but to initialize it, the variable is undefined, for example
1 var message; 2 3 // true
Null type
The nulll type is the second data structure with only one value, and this particular value is null. From a logical point of view, a null value represents an empty object pointer, which is why the TypeOf operator displays "object" when it detects null values
1 var car=null; 2 alert (typeof car); // "Object"
Boolean type
It is one of the most used types, with only two literals: true and false.
To convert a value to its corresponding Boolean value, you can call the Transform function Boolean ()
var message= "Hello World";
var messageasboolean=boolean (message);
In this example, the string message is converted to a Boolean value that is stored in the Messageasboolean variable. You can call the Boolean () function on a value of any data type, and always return a Boolean value.
Number Type
According to the ECMAScript standard, there is only one numeric type in JavaScript: The value of a double-precision 64-bit binary format based on the IEEE 754 standard (-(253-1) to 253-1). it does not give a specific type for integers . In addition to being able to represent floating-point numbers, there are some signed values: +Infinity
, -Infinity
and NaN
(non-numeric, not-a-number).
String type
The string type of avascript is used to represent text data. It is an "element" of a set of 16-bit unsigned integer values. Each element in the string occupies the position of the string. The index of the first element is 0, the next is index 1, and so on. The length of the string is the number of its elements.
Unlike the class C language, JavaScript strings are immutable. This means that once a string is created, it cannot be modified. However, you can create a new string based on the operation of the original string. For example:
Gets a substring of a string that can be selected by selecting individual letters or using String.substr()
.
Two string connections using the Join operator ( +
) or String.concat()
.
Object type
An object is actually a set of data and functions.
objects can be created by executing the new operator followed by the name of the object class to be created. For example:
1 var o=New object ();
However, it is not useful to just create an instance of object, because any properties and methods that the object type has also exist in more specific objects.
Each instance of object has the following properties and methods.
1.constructor: Used to save the function used to create the current object. For the previous example, the constructor is object ().
2.hasOwnProperty (PropertyName): Used to check whether a given property exists in the current object instance. Example: O.hasownproperty ("name")
3.isPrototypeOf (object): Used to check if incoming formations are prototypes of incoming objects
4.toLocaleString (): Returns the string representation of an object that corresponds to the region where the environment is being executed.
5.toString (): Returns the string representation of the object.
6. ValueOf (): Returns the string, numeric value, or Boolean representation of the object.
This article refers to JavaScript Advanced Programming (Third edition)
JavaScript Data Structures