Introduction to the concepts of commonly used data types in JavaScript
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
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 新定义)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 typeThe Null type has only one value: null. For more details, see null and Null.
Undefined typeA variable that has not been assigned a value will have a default value of undefined. For more details, see undefined and Undefined.
Number typesAccording 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
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!

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft