Home > Web Front-end > JS Tutorial > Introduction to the concepts of commonly used data types in JavaScript

Introduction to the concepts of commonly used data types in JavaScript

不言
Release: 2019-03-18 10:51:13
forward
2402 people have browsed it

This article brings you an introduction to the concepts of commonly used data types in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Programming languages ​​all have built-in data structures, but the data structures of various programming languages ​​are often different. This article attempts to list the built-in data structures and their properties in the JavaScript language, which can be used to construct other data structures; and try to describe the differences from other languages ​​as much as possible.

1. Dynamic typing

JavaScript is a weakly typed or dynamic language. This means that you don't need to declare the type of the variable in advance, the type will be determined automatically during the execution of the program. This also means that you can use the same variable to save different types of data:

JS Common Data Types

Programming languages ​​have built-in data structures, but the data structures of various programming languages There are often differences. This article attempts to list the built-in data structures and their properties in the JavaScript language, which can be used to construct other data structures; and try to describe the differences from other languages ​​as much as possible.

1. Dynamic typing

JavaScript is a weakly typed or dynamic language. This means that you don't need to declare the type of the variable in advance, the type will be determined automatically during the execution of the program. This also means that you can use the same variable to save different types of data:

var foo = 42;    // foo is a Number now
foo = "bar"; // foo is a String now
foo = true;  // foo is a Boolean now
Copy after login

The characteristic of dynamically typed language is flexibility, but the disadvantage is that it sacrifices some performance. For dynamically typed languages, variable types can be changed dynamically and cannot be determined at compile time. Therefore, the type checking at compile time is relatively weak, which will lead to many type errors that cannot be discovered until runtime.

2. Data types

The latest ECMAScript standard defines 7 data types:

6 primitive types:

   Boolean
   Null
   Undefined
   Number
   String
   Symbol (ECMAScript 6 新定义)
Copy after login
Object

3. Primitive value ( primitive values ​​)

All types except Object are immutable (the value itself cannot be changed). For example, unlike the C language, strings in JavaScript are immutable. We call these types of values ​​"primitive values".

Boolean type (Boolean)

Boolean represents a logical entity and can have two values: true and false.

Null type

The Null type has only one value: null. For more details, see null and Null.

Undefined type

A variable that has not been assigned a value will have a default value of undefined. For more details, see undefined and Undefined.

Number types

According to the ECMAScript standard, there is only one number type in JavaScript: a double-precision 64-bit binary format value (-(263 -1) to 263 -1) based on the IEEE 754 standard. It does not give a specific type for integers. In addition to being able to represent floating point numbers, there are also signed values: Infinity, -Infinity and NaN (Not-a-Number).

To check whether a value is greater or less than /-Infinity, you can use the constants Number.MAX_VALUE and Number.MIN_VALUE. In addition, in ECMAScript 6, you can also use the Number.isSafeInteger() method as well as Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER to check whether the value is within the range of double-precision floating point numbers. Beyond this range, numbers in JavaScript are no longer safe, i.e. only second mathematical interger can be represented correctly in JavaScript number types.

The numeric type has only one integer, which has two representation methods: 0 can be represented as -0 and 0 ("0" is the abbreviation of 0). In practice, this also has little impact. For example 0 === -0 is true. However, you may want to pay attention when dividing by 0:

42 / +0; // Infinity
42 / -0; // -Infinity
Copy after login

String type

JavaScript's string type 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 a position in the string. The first element has index 0, the next has index 1, and so on. The length of a string is the number of its elements.

Unlike C-like languages, JavaScript strings are immutable. This means that once the string is created, it cannot be modified. However, it is possible to create new strings based on operations on the original string. For example:

Get a substring of a string by selecting individual letters or using String.substr(). To connect two strings, use the concatenation operator ( ) or String.concat().

Pay attention to the "string type" in the code!
Strings can be used to express complex data. Here are some great properties:

Easy to concatenate and construct complex string characters
Strings are easy to debug (what you see is often in the string)
Strings are usually a common standard in many APIs (input fields, local storage values, XMLHttpRequest responds when using responseText etc.) and it can only be used with strings.
By convention, strings can generally be used to express any data structure. This is not a good idea. For example, using a delimiter, one can mimic a list (a JavaScript array might be more suitable). Unfortunately, when a delimiter is used for elements in a list, it disrupts the list. an escape character, etc. All of these routines become a non-existent maintenance burden without the right tools to use.

It is recommended to use strings when expressing text data and symbolic data. Use string parsing and appropriate abbreviation when expressing complex data.

Symbol type

Symbols are newly defined in ECMAScript 6th edition. Symbolic types are unique and unmodifiable, and can also be used as the key value of Object (as shown below). There are also similar atomic types (Atoms) in some languages. You can also think of them as being in C Enumerated types. See Symbol and Symbol for more details.

Object Object

In Javascript, an object can be viewed as a collection of properties. When an object is defined using object literal syntax, a set of properties is automatically initialized. (In other words, if you define a var a = "Hello", then a itself will have the method a.substring, the attribute a.length, and others; if you define an object, var a = {}, Then a will automatically have properties and methods such as a.hasOwnProperty and a.constructor.) Then, these properties can be increased or decreased. The value of a property can be of any type, including objects with complex data structures. An attribute is identified by a key, and its key value can be a string or a symbol value (Symbol).

There are two types of properties in objects defined by ECMAScript: data properties and accessor properties.

Data attributes
Data attributes are key-value pairs, and each data attribute has the following characteristics:

Attributes of a data property

O(∩_∩)O Haha~ The basics are still very important.



The above is the detailed content of Introduction to the concepts of commonly used data types in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template