Javascript data type and value (Basic Edition)

Source: Internet
Author: User

Javascript allows three basic data types: Numbers, text characters, and boolean values. In addition, it supports two small data types: null and underfined, each defining only one value. In addition to these basic data types, JavaScript also supports conforming data types-objects ). Javascript also defines another special object-function ). In addition to functions and arrays, the Javascript language core also defines other specialized objects. These objects are not new data types, but new classes of objects ). The date class defines objects that represent dates, the Regexp class defines objects that represent regular expressions, and the error class defines syntactic error objects that occur in javascript programs.

  Number

The numeric types of JavaScript do not distinguish integer and floating-point data. (64-bit floating point number)

6.02e23 // 6.02x10 to the power of 23

1.4738e-32 // 1.4738223*10 minus 32 power

Javascript uses the arithmetic operators provided by the language to perform numerical operations. These operations include (+ ,-,*,/)

Javascript also uses some special values.

Infinity indicates a special value of infinity.

Nan special non-numeric value

Number. max_value indicates the maximum number.

Number. min_value indicates the smallest number.

Number. Nan special non-numeric value

Number. positive_infinity indicates the special value of positive infinity.

Number. negative_infinity indicates the special value of negative infinity.

  String

A string (sting) is a sequence of Unicode characters enclosed by single quotes or double quotes. It contains 0 or more Unicode characters. A string that is bounded by single quotes can contain double quotation marks, and a string that is bounded by double quotation marks can also contain single quotation marks. (If you want to add a line break to a string, use/n)

 

   <a href="contact.html" onclick="alert('test')">1</a>   <a href="contact.html" onclick='alert("test")'>2</a>

 

MSG = "hello", + "world"; // "Hello, world"

Determine the length of a string, S. Length

To obtain the last character of a string. Last_char = S. charat (S. Length-1)

You can extract the second, third, and fourth characters from the string. Sub = S. substing (1, 4)

Find the position I = S. indexof ('A') of the first letter "a" from the string ');

Converts a number to a string.

If a number is used in a string connection expression, the number is converted to a string: var n = 100; var S = N + "";

Or var string_value = string (number); string_value.tosting ();

VaR n = 17

Binary_string = n. tosring (2); // 2 hexadecimal 10001

Octal_string = n. tosring (8); // octal 021

Hex_string = n. tosring (16); // hexadecimal 0x11

Tofixed () converts a number to a string and displays the specified number of digits after the decimal point. Toexponential () converts a number into a string using an exponential method. Toprecision () uses the specified meaningful number of digits to display the number of the instrument. If the meaningful number is not enough to display the entire integer, it uses an exponential representation (all three methods are rounded off)

Var n= 123456.789

N. toFixed (0); // 123456.789

N. toFixed (2); // 123456.79

N. toExponential (1); // 1.2e + 5

N. toExponential (3); // 1.235e + 5

N. toPrecision (4); // 1.235e + 5

N. toPrecision (7); // 123456.8

Converts a string to a number.

When a string is used in a numeric environment, it is automatically converted to a number. Var product = "21" * "2 ";

With this advantage, we can convert a string to a number var number = "21"-0 by subtracting 0;

Var number = Number ("21"); this method is too strict. It is only valid for a number with a base of 10. In a string with a followed digit, it cannot contain any non-null characters. To allow more flexible conversions, you can use parseInt () and parseFloat (). ParseInt () Only truncates integers. ParseFloat () Intercepts integers and floating-point numbers.

ParseInt ("3 is int"); // 3

ParseInt ("12.34 is int"); // 12

ParseInt ("0xff"); // 255

ParseFloat ("3.14 is float") // 3.14

ParseInt ("11", 2); // 3 parseInt () You can specify one more parameter to parse the base number.

But parseInt () and parseFloat () cannot convert the specified string to a number, and it will return NaN

Parseint ("one"); // Nan

Parsefloat ("$72.47"); // Nan

Boolean Value

  Boolean data has only two values. These two valid values are expressed by the direct quantity of true and false, respectively.

Boolean values are easily converted. If a Boolean value is used in a numerical environment, true is converted to 1, and false is converted to 0, true is converted to the string "true", and false is converted to the string "false ". If a number is used in a place where it is supposed to be a Boolean value, it is 0 or Nan, And it is converted to false, otherwise it will be true. if a string is used in a Boolean value, the empty string is converted to false. Otherwise, the string is converted to true. Null or undefined values are also converted to false, and any non-empty objects, arrays, or functions are converted to true.

VaR x = boolern (X );

VaR x = !! 1;

  Function

A function is an executable JavaScript code segment defined by a javascript program or predefined by JavaScript. Although the function is defined only once, the Javascript program can execute or call it multiple times. A JavaScript function can contain either a substantive parameter or a formal parameter, which is used to specify that one or more values are used for execution and calculation of this function. In addition, it can return a value.

Javascript can define and call functions. The Code is as follows:

 

        function sum(x) {            return x + x;        }        alert(sum(2).toString());

 

In many languages, a function is just a syntax feature of the language. It can be defined and called, but not a data type. But the real numeric value of the functional expression in Javascript brings great flexibility to the language. This means that value functions can be stored in variables, arrays, and objects, and functions can also be passed as parameters to other functions. This is very useful

The direct function quantity is defined by adding an optional function name after the function keyword and the function body enclosed in parentheses.

 

        var sum = function (x) { return x + x };        alert(sum(1).toString());

Object

An object is a set of named data. These named data are usually referenced as object attributes. To reference an object's attributes, you must reference this object and add the solid point and attribute name after it. For example, document. Write ("this a test! "); Or document. Body. firstchild

Creates an object by calling special constructors.

VaR o = new object ();

VaR now = new date ();

VaR pattern = new Regexp ("\ sjava \ s", "I ");

Once you create an object of your own, you can design and use its attributes as needed:

VaR point = new object ()

Point. x = 2.3;

Pont. Y = 1.2;

Javascript defines the syntax of the object's direct quantity, so that you can create an object and define its attributes.

VaR Pont = {X: 2.3, Y:-1.2}

The direct quantity of objects can also be nested;

VaR AAA = {BBB: {X: 2, Y: 2}, CCC: {X: 4, Y: 4 }};

Finally, it should be noted that the attribute value used in the direct object quantity does not need to be a constant. It can be any JavaScript expression. In addition, the property name in the object's direct quantity can be a string rather than an identifier:

VaR user = {"username": "Andy ",
"Age": 20,
"Info": {"tel": "123456", "cellphone": "98765 "},
"Address ":
[
{"City": "beijing", "postcode": "222333 "},
{"City": "newyork", "postcode": "555666 "}
]

}

Isn't that Jason?

Array

Arrays and objects are the same set of values. The difference is that each value in the object has a name, and no number or subscript (index) of the array ).

Document. images [1]. width

Note that JavaScript does not support multi-dimensional arrays, but its array elements can also be arrays. Since Javascript is a non-typed language, array elements do not have to have the same type.

You can use the constructor array () to create an array. Once an array is created, you can easily assign values to any element of the array:

VaR A = new array ();

A [0] = 1.2;

A [1] = "JavaScript"

A [2] = true;

A [3] = {X: 1, Y: 3}

An array is a sequence in a closed square brackets. Elements in the sequence are separated by commas. The values in the brackets are assigned to the array element in sequence, and the lower value starts from 0.

VaR A = [1.2, "JavaScript", true, {X: 1, Y: 3}];

Arrays can also be nested like the Direct Volume of objects:

VaR martrix = [[, 3], [, 6], [, 9];

In addition, like an object, elements in an array element do not have to be limited to constants. It can be any expression.

VaR base = 1024;

VaR table = [base, base + 1, base + 2, base + 3];

You can also store unspecified elements in the array directly, as long as the value of this element is removed between commas.

VaR A = [1.2, {X: 1, Y: 3}]; // two elements are not defined;

  Null

The javascript keyword null is a special value, which indicates "no value ". Null is often seen as a special value of the object type, representing the value of "no object. Null is a unique value, different from all other values. If the value of a variable is null, it indicates that its value is not a valid object, array, number, string, or Boolean value. If null is used in a Boolean environment, it is converted to false. When it is used in the numeric environment, it is converted to 0. When it is used in a string environment, it is converted to "null ".

  Underfined

Underfined is returned when you use a variable that has not been declared, when you use a variable that has been declared but has not been assigned a value, or when you use an object attribute that does not exist. Although the values of underfined and null are different, the = operation equals the two.

When an unspecified value is used in a Boolean environment, it is converted to false: Used in a digital environment, and it is converted to NaN. When used in the string environment, it is converted to "undefined ".

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.