This article takes you into the world of js data types and data structures

This article takes you into the world of js data types and data structures

1. What is dynamic typing?

  • JavaScript is a weakly typed or dynamic language.
  • We do not need to declare the type of the variable in advance, the type will be automatically determined during the program execution.
  • This also means that you can use the same variable to store different types of data.
let a; // Initially no type is given a = 11; // a is now of type number a = "二大爷"; // a becomes a string type a = true; // a becomes a Boolean type

It is not recommended to do this in actual application, as it may cause some unpredictable errors.

  • For example, your brother suddenly becomes a woman, and is very beautiful ( To sleep or not to sleep? )
  • For example, your best friend suddenly becomes a man, very handsome ( To go or not to go? )
  • For example, the neighbor Mr. Wang becomes your wife... (Mr. Wang himself becomes a cuckold?)
  • To sum up, once the data type is determined, do not tamper with it easily ~~

insert image description here

2. Data Types

  • You may still think that JavaScript only has 6 data types (this is the inherent perception)
  • The latest ECMAScript standard defines 8 data types :

2.1 Primitive types (6 primitive types, checked using the typeof operator)

  • undefined: typeof instance === "undefined"
  • Boolean: typeof instance === "boolean"
  • Number: typeof instance === "number"
  • String: typeof instance === "string"
  • BigInt: typeof instance === "bigint" ( arbitrarily large integer )
  • Symbol: typeof instance === "symbol"

2.2 null and Object

  • null: typeof instance === "object"
  • typeof instance === "object".
  • Any special non-data structure type that is an instance of a constructed object, also used as a data structure : (new Object, new Array, new Map, new Set, new WeakMap, new WeakSet, new Date), and almost anything created with the new keyword.

2.3 Key points of typeof operator

  • The sole purpose of the typeof operator is to check data types.
  • If we want to check the type of any structure derived from Object, using typeof will not work because we will always get "object".
  • The proper way to check the type of an object is to use the instanceof keyword (array vs object, etc.), but even this has inaccuracies (there is no 100% ready method at the moment, unfortunately)

insert image description here

3. Original value

3.1 Basic Concepts of Primitive Values

  • All types except Object are immutable (the value itself cannot be changed).
  • For example, unlike C, strings in JavaScript are immutable ( operations on strings in JavaScript always return a new string, and the original string is not changed ). We call values ​​of these types "primitive values".

3.2 Description of each type

  • Boolean type : a logical entity that can have two values: true and false
  • Null type : has only one value: null, which means nothing
  • Undefined type : A variable that has not been assigned a value will have a default value of undefined
  • Number type : There is no further subdivision like Java, both integers and floating point numbers are possible. There are also some signed values: +Infinity, -Infinity, and NaN (Not-a-Number)
  • BigInt type : A basic numeric type in JavaScript that can represent integers with arbitrary precision. Large integers can be safely stored and manipulated, even those exceeding the safe integer limit for numbers. A BigInt is created by appending n to the end of an integer or by calling the constructor.

Syntax: BigInt(value);

value: The value of the created object. Can be a string or an integer

BigInt() is not a constructor, so the new operator cannot be used.

  • String type : Strings are immutable. This means that once a string is created, it cannot be modified. However, it is possible to create new strings based on operations on original strings.

You can get a substring of a string by selecting individual letters or using String.substr().

To concatenate two strings use the concatenation operator (+) or String.concat().

  • Symbols type : unique and unmodifiable, and can also be used as the key value of an Object
  • Object type : 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.

The value of a property can be of any type, including objects with complex data structures. Attributes are identified by keys, whose values ​​can be strings or symbols.

The above is the detailed content of this article that takes you into the world of js-data types and data structures. For more information about js data types and data structures, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of basic data types in js
  • Eight JavaScript data types
  • Detailed explanation of the seven data types in JavaScript
  • Examples of correct judgment methods for data types in JS
  • Detailed explanation of JavaScript data type conversion (recommended)
  • Let's take a look at the most detailed explanation of JavaScript data types

<<:  Solution to the problem of returning 0x1 when the Windows 2008 task plan fails to execute a bat script

>>:  mysql replace part of the field content and mysql replace function replace()

Recommend

HTML table markup tutorial (16): title horizontal alignment attribute ALIGN

By default, the table title is horizontally cente...

Summary of MySQL password modification methods

Methods for changing passwords before MySQL 5.7: ...

Detailed process of installing various software in Docker under Windows

1. Install MySQL # Download mysql in docker docke...

Correct use of Vue function anti-shake and throttling

Preface 1. Debounce: After a high-frequency event...

Detailed steps for deploying Tomcat server based on IDEA

Table of contents Introduction Step 1 Step 2: Cre...

MySQL foreign key constraint (FOREIGN KEY) case explanation

MySQL foreign key constraint (FOREIGN KEY) is a s...

How to fix the four sides of the table to scroll up, down, left and right

question: When I was doing project statistics rec...

Security configuration and detection of SSL after the website enables https

It is standard for websites to enable SSL nowaday...

Detailed explanation of the background-position percentage principle

When I was helping someone adjust the code today,...

CSS to achieve compatible text alignment in different browsers

In the front-end layout of the form, we often nee...

Detailed steps for setting up and configuring nis domain services on Centos8

Table of contents Introduction to NIS Network env...

Analysis of the difference between HTML relative path and absolute path

HTML beginners often encounter the problem of how ...

Chrome monitors cookie changes and assigns values

The following code introduces Chrome's monito...