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

Mar 18, 2019 am 10:51 AM
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
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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

See all articles