JavaScript data types

Source: Internet
Author: User

JavaScript's data types fall into two categories: primitive types and Object types. The original type includes numbers, strings, and Booleans, with two special primitive values, null and undefined, but they are not numbers, strings, and Boolean values. In addition to the original type, the object is a collection of properties, each property consists of a "name/value pair", the ordinary object is an unordered collection, but the array object is an ordered set, the function is an object, the calling function can run executable code, and there are several useful class objects, which are date, regular, and error. JavaScript's data types can also be categorized as types that have methods and types that cannot own methods, or are mutable and immutable types. Primitive types are immutable types, and object types are mutable or referred to as reference types.

The data types of JavaScript are freely convertible, the variables are untyped or dynamic, declaring variables without specifying the variable type, the same variable can be assigned different types of values, you can use the VAR keyword to declare the variable, is a common type. JavaScript uses lexical scopes, where variables declared within a function have a function scope, variables that are not declared in any function are global variables, and are visible anywhere in the JavaScript program. JavaScript can automate many types of conversions, especially when it comes to operators, but sometimes you still need to do explicit conversions, all of which inherit two conversion methods, toString () and valueof (), and here are the idioms for several types of conversions:

""// 等价于String(x)// 等价于Number(x) 也可以谢谢x-0// 等价于Boolean(x) 使用了两个叹号

The JavaScript interpreter has its own memory management mechanism that can automatically recycle memory, which means that the program can create objects on demand, and we don't have to worry about destroying and recycling these objects, and when there are no longer any references to an object, the interpreter will know that the object is useless. It then automatically reclaims the memory resources it consumes.

JavaScript is an object-oriented language, with its own methods, however, as a non-object type of numbers, characters and Booleans can also have their own methods, they all have one of the same method is ToString (), but can not own the method is null and undefined.

Number--

JavaScript does not differentiate between integer values and floating-point values, and all numbers are represented by floating-point number. JavaScript provides some common basic operators, and complex operations can be implemented through the Math object. JavaScript pre-defines global variables infinity and Nan, which are used to represent both infinity and non-numeric values. Because the floating-point notation of JavaScript is a binary representation method, it is important to be careful in representing decimal fractions to ensure accuracy. A Date object is a commonly used object that is related to dates and times.

Text--

JavaScript uses the UTF-16 encoded Unicode character set, which is a sequence of unsigned 16-bit values. The JavaScript string supports single and double quotes, which can be nested with each other but appear in pairs. A connection to a JavaScript string can use the plus sign, which supports subscript access, and is actually a read-only group, because any modification will result in a new string. Pattern matching can use the RegExp object, or you can use a regular regular expression, and the text between two slashes forms a direct amount of the regular expression.

Boolean Value--

Any JavaScript value can be converted to a Boolean value, converted to false with undefined, NULL, 0,-0, Nan, and an empty string, and all other values are converted to ture. Null is a keyword in the JavaScript language that represents a special value that is commonly used to describe "null", while undefined is a predefined global variable, but not a keyword, whose value is "undefined".

Global objects--

At the top of the JavaScript code, which is the JavaScript code that is not inside any function, you can use the keyword this to refer to the global object.

Packaging Objects--

When a string is defined, it is not an object, but it can be accessed through a period because the property is accessed by a temporary string object, which is called a wrapper object, similar to a number and Boolean value.

Variable--

Declaring a variable uses the var keyword, although you can write code in the global scope without writing the Var statement, but when declaring a local variable, you must use the VAR statement, which obscures the global variable with the same name, but the global variable is directly manipulated when you do not use Var. When you declare a JavaScript global variable, you actually define a property of the global object, and when you declare a variable with VAR, the created property is not configurable, that is, the variable cannot be deleted by the delete operator. Instead of using Var, you can delete it. Each piece of JavaScript code has a scope chain associated with it, which is a list of objects or lists of objects that define the variables in the scope of this code. One of the attributes of a variable declaration is "declaration in advance," and here's an interesting example:

var"global";function f() {// "undefined"var// "local"}

JavaScript data types

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.