Home > Web Front-end > JS Tutorial > Introduction to JavaScript data types

Introduction to JavaScript data types

不言
Release: 2019-03-23 09:33:47
forward
2181 people have browsed it

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
Copy after login

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
Copy after login

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.3This 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
Copy after login

There are three special brothers in the Number type

  1. Infinity When the value exceeds the maximum value of the Number type, it will become positive infinity
  2. -Infinity When the numerical type is less than the minimum value of the Number type, it will become negative infinity
  3. 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
Copy after login

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'
}
Copy after login
rrree

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
Copy after login

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 // 男
Copy after login

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!

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