Introduction to JavaScript data types
This article brings you a detailed introduction to JavaScript data types. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
I recently interviewed three developers, and none of them could explain clearly what the basic types of JS are. And they often mistakenly mention some C language data types, such as int, float, double and other data types.
No matter what language, proficiency in data types is the most fundamental knowledge point of this language
JS data types are divided into two categories, one is the basic type, They have
- Number
- String
- Boolean
- Undefined
- Null
- Symbol
There are only a few basic types of js. In addition, other types are objects.
Number type
There are no integer, float, or double type values in JS. All values are called Number type.
JS uses IEEE754 format to represent integers and floating point numbers. Generally speaking, integers occupy 32 bits, while floating point numbers occupy 64 bits. Because floating point numbers occupy twice the memory space of integers, js will appropriately convert floating point numbers into integers for storage.
4.0 === 4 // true
Numeric types all have a size range
Number.MAX_VALUE // 1.7976931348623157e+308 Number.MIN_VALUE // 5e-324 Number.MAX_SAFE_INTEGER // 9007199254740991 Number.MIN_SAFE_INTEGER // -9007199254740991
Floating point numbers generally have inaccurate calculation results. This is not a JS problem, this problem exists in all languages.
Calculation0.1 0.2 === 0.3
This result is always false.
To compare whether floating point numbers are equal, you can use Number.EPSILON
. Number.EPSILON is a very small value. If the two floating point numbers are smaller than Number.EPSILON, you can are considered equal.
Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON // true
There are three special brothers in the Number type
- Infinity When the value exceeds the maximum value of the Number type, it will become positive infinity
- -Infinity When the numerical type is less than the minimum value of the Number type, it will become negative infinity
- NaN NaN means that it is not a numerical value. For example, if a certain value is divided by 0, an error will be reported in ordinary languages, but js will not. Instead, the value of the variable will become NaN.
Once the numerical type becomes these three brothers, it cannot participate in subsequent numerical operations.
String type
Students who come from static languages will ask a question when they encounter a string: How many strings can you hold?
The string smiled silly and said: Since I was born, it has never been filled!
ECMAScript 2016 (ed. 7) established a maximum length of 2^53 - 1 elements. Previously, no maximum length was specified. In Firefox, strings have a maximum length of 230 - 2 (~1GB). In versions prior to Firefox 65, the maximum length was 228 - 1 ( ~256MB). --MDN
Boolean
Boolean value is very simple, just two values: false and true. But many people can't fully answer which values will be converted to false.
Except for the following values that can be converted to false, all others are true.
false '' NaN undefined 0, -0, +0 null
undefined and null
undefined indicate that a variable is defined but has not been assigned a value. null means that the variable is not defined at all. In short, whether it is undefined or null, they are basically unusable values.
The null type has a special function. For example, there is an object with many attributes. If you want to mark this variable as ready for garbage collection, you can set its value to null.
The most familiar stranger: Object
I once thought that objects were the simplest in js, but in fact, I was too naive.
// 定义一个对象,so easy var boy = { name: 'wangduanduan' }
Data attributes of the object
Data attributes | Default value | Description |
---|---|---|
configurable | true | Indicates whether this attribute can be deleted using delete |
enumerable | true | Indicates whether this attribute can be traversed through a for in loop |
writable | true | Indicates whether this attribute can be traversed Modified |
value | undefined | Indicates the data value of this attribute |
If When calling Object.defineProperty without specifying configurable, enumerable, or writable, their default values are all false.
Accessor attribute
The accessor attribute is get, and set allows you to do a layer of interception when reading or writing values.
var boy = {} Object.defineProperty(boy, 'name', { writable: false, value: 'wdd' }) boy.name = 'ddw' // 设置不会生效,boy.name的值还是wdd
Think about what would happen if you change _sex above to sex?
var man = { _sex: 1 } Object.defineProperty(man, 'sex', { set: function (v) { this._sex = v === '男' ? 1 : 0 }, get: function () { return this._sex === 1 ? "男" : "女" } }) nam.sex // 男
This article has ended here. For more exciting content, you can pay attention to the JavaScript Video Tutorial column on the PHP Chinese website!
The above is the detailed content of Introduction to JavaScript data types. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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 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 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.

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 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

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

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).

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
