Data types in JavaScript

Source: Internet
Author: User

First, the summary

The data types in JavaScript fall into two categories:

Simple Type: boolean,number,string

Reference type: Object

Other: Undefined represents variable not initialized, null means NULL for reference type, TYPEOF null returns object

1) Differences in simple types and reference types, similar to other languages, such as storage locations

2) for the three simple types of boolean,number,string, JavaScript also provides the corresponding basic wrapper type, the difference between the basic wrapper type and the reference type is the life cycle of the object, and when accessing the base type value in Read mode, Background pairs of objects that are automatically created with a basic wrapper type, provide a way to manipulate the base type, but once the statement that operates the base type value is executed, the newly created basic wrapper type is immediately destroyed.

3) The function is also an object, which is not the same as the Java language, so the function can also have a method, can be used as a function parameter.

4) Determine the element type: Use the typeof operator (the return value may be boolean,number,string,object,function,undefined), determine which object the reference type is an instance of, and use the isinstanceof

II. Basic Types

var a= literal form to define the base type

1.Boolen

1) with two literal true,false (note case sensitive)

2) Convert other types of values to Boolean values, using Boolen (Var), and specific conversion rules can be found in the textbook. If an empty string is converted to false

3) in the IF () judgment, the Boolean conversion occurs automatically.

2.Number type

1. There are binary differences, with integers and floating point numbers. Definitions are adapted to the form of the Var a= literal

2.number.min_value represents the minimum value, Number.MAX_VALUE represents the maximum value, global attribute infinity represents positive infinity,-infinity represents negative infinity, Nan represents non-number, and isNaN () can be used to determine

3, convert any other type to a numeric type you can use the number () function to convert a string to a numeric type using parseint (VAR, binary) or parsefloat (Var)

4, specify the number of decimal places you can use the NUM variable. toFixed (3), the conversion to an exponential type means that the NUM variable can be used. Toexponetial ()

5,string type

1,length Property: String length

2, to convert a value to a string, you can call ToString (), the simplest is to add an empty string

string-related operation methods:

1, string concatenation, using "+", also available concat () method

2, String cutting, slice (), substr (), substring () method, usage is similar, master one can

5, Find substring: indexof () and LastIndexOf (); Another lookup method is search (), the argument can be a string or a regular expression

6, delete space before and after: trim ()

7, Case conversion: toLowerCase () and toUpperCase ()

8, regular match: Match () method, the call effect is the same as the Exec () method of the regular object regexp

9, String substitution: Repalce (), the first argument can be a string or a regular expression, if it is a character channeling, then only the first substring is replaced, if it is a regular, you can replace all matching substrings.

10, string comparison: Because strings are basic types, you can use "= =" to compare; ' = = ' is strong. Another method is the Localcompare () method, which returns a positive number, 0, or a negative number, depending on the sort size in the alphabet.

11, String Cut: Split (). The delimiter can be a substring, or it can be a regular

12. Access specific characters in the string: CharAt () or charCodeAt (); Returns the corresponding character or character encoding

Iii. reference types

The reference type is either object or its subclass (JS actually does not have the concept of subclasses?). ), JS has built-in reference types to facilitate operations, such as Date,array,math,regexp, which are properties of the Global Object window. All object types have the following properties or methods

Constuctorhasownproperty (PropertyName) isprototypeof(object) propertyisenumerable ( PropertyName) toString () valueOf () tolocalstring ()

1.object type

I feel that the object type in JS is more like the HashMap in Java or the dict type in Python, which is the sense of key-value pairs.

1) New, two methods, constructors and literals

// methods of constructors var person=New  Object ();p erson.name="bobo";p erson.age =; // the method of literal quantity var person={  name="bobo",  age =  (  }

2) attributes can be quoted or not added, access attributes can be expressed in dot notation, or square bracket notation

2.Array type

1. New, two methods, constructors and literals

// constructor Function var list=New  Array (); // literal quantity var list={1,2,3}

In contrast to arrays in Java and other languages, each item in a JS array can hold different types of variables, and the size can be dynamically adjusted.

To detect if a variable is an array, you can use the Array.isarray method

2. Related properties and methods

1) Array Length: Length, this property is not read-only, by setting this property, you can remove the item (set value < actual length) from the end of the array, or add an item to the array

2) Convert the array to a string: join ("separator")

3) Related Stack method: Pusth (), pop (), Shift (), push (), Unshift (), pop () through these methods, you can use an array to simulate a queue or stack

Data types in JavaScript

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.