What are the data types of JavaScript? Introduction to js data types
This article brings you what are the data types of JavaScript? The introduction of js data types has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. How many types of values does JavaScript have?
Javascript has two data types, namely basic data types and reference data types. The basic data types include Undefined, Null, Boolean, Number, String, and Symbol (New in ES6, representing unique values), and reference data types are collectively called Object objects, which mainly include objects, arrays and functions. Next, let’s look at the characteristics of the two respectively.
2. Basic data types
1. The value is immutable
var name = 'java'; name.toUpperCase(); // 输出 'JAVA' console.log(name); // 输出 'java'
It can be concluded that the value of the basic data type is immutable ’s
2. Stored in the stack area
The original data type is a simple data segment directly stored in the stack. It occupies a small space and has a fixed size. It is frequently used data, so it is placed Store on the stack.
3. Value comparison
var a = 1; var b = true; console.log(a == b); // true console.log(a === b); // false
==: Only value comparison is performed, and data type conversion is performed.
===: Not only compare values, but also compare data types.
3. Reference data types
1. The value is variable
var a={age:20}; a.age=21; console.log(a.age)//21
The above code shows that reference types can have properties and methods, and are Can be changed dynamically.
2. Saved in stack memory and heap memory at the same time
The reference data type is stored in the heap (heap), which occupies a large space and is not fixed in size. If it is stored in the stack, it will Will affect the performance of the program; the reference data type stores a pointer on the stack, which points to the starting address of the entity in the heap. When the interpreter looks for a reference value, it first retrieves its address on the stack and then obtains the entity from the heap.
3. Comparison is a reference comparison
When assigning a reference type value from one variable to another variable, the same Also copies the value of the object stored in the variable into the space allocated for the new variable.
var a={age:20}; var b=a; b.age=21; console.log(a.age==b.age)//true
We mentioned above that basic types and reference types are stored in different locations in memory. The reference type is an object stored in the heap. At the same time, a pointer is stored in the stack, and this pointer points to the heap. The starting position of the entity in the object. When variable a is initialized, the pointer a points to the address of object {age:20}. After a is assigned to b, b points to the address of object {age:20}. These two variables point to the same object. Therefore, changing any one of these variables will affect each other.
At this time, if a variable references the original object, it will not affect the other variable.
var a={age:20}; var b=a; a = 1; b // {age:20}
In the above code, a and b point to the same object, and then the value of a changes to 1. This will not affect b, and b still points to the original object.
4. Check the data type
1.typeof
typeof returns a string representing the data type. The returned results include: number, boolean, There are 7 data types such as string, symbol, object, undefined, function, etc., but null, array, etc. cannot be judged.
typeof Symbol(); // symbol 有效 typeof ''; // string 有效 typeof 1; // number 有效 typeof true; //boolean 有效 typeof undefined; //undefined 有效 typeof new Function(); // function 有效 typeof null; //object 无效 typeof [] ; //object 无效 typeof new Date(); //object 无效 typeof new RegExp(); //object 无效
Arrays and objects return objects. In this case, you need to use instanceof to judge
2.instanceof
instanceof is used to determine whether A is an instance of B. The expression is: A instanceof B. If A is an instance of B, it returns true, otherwise it returns false. The instanceof operator is used to test whether an object has a constructor's prototype property in its prototype chain.
[] instanceof Array; //true {} instanceof Object;//true new Date() instanceof Date;//true new RegExp() instanceof RegExp//true
Regarding the type judgment of arrays, you can also use ES6 to add Array.isArray()
Array.isArray([]); // true
instanceof Three major disadvantages:
For basic In terms of data types, there are certain differences between the results created by literals and those created by instances.
console.log(1 instanceof Number)//false console.log(new Number(1) instanceof Number)//true
Strictly speaking, only the results created by instances are standard object data type values. , is also an instance of the standard Number class; the result created by the literal method is a basic data type value, not a strict instance. However, due to the loose characteristics of JS, the methods provided on Number.prototype can be used.
As long as it is on the prototype chain of the current instance, the results we detect using it are all true. In the prototypal inheritance of a class, the final result we detect may not be accurate.
var arr = [1, 2, 3]; console.log(arr instanceof Array) // true console.log(arr instanceof Object); // true function fn(){} console.log(fn instanceof Function)// true console.log(fn instanceof Object)// true
Cannot detect null and undefined
For the special data types null and undefined, their classes are Null and Undefined, but the browser protects these two classes and does not allow them We use it for outside visits.
3. Strict operator ===
只能用于判断null和undefined,因为这两种类型的值都是唯一的。
var a = null typeof a // "object" a === null // true
undefined 还可以用typeof来判断
var b = undefined; typeof b === "undefined" // true b === undefined // true
4.constructor
constructor作用和instanceof非常相似。但constructor检测 Object与instanceof不一样,还可以处理基本数据类型的检测。
var aa=[1,2]; console.log(aa.constructor===Array);//true console.log(aa.constructor===RegExp);//false console.log((1).constructor===Number);//true var reg=/^$/; console.log(reg.constructor===RegExp);//true console.log(reg.constructor===Object);//false
constructor 两大弊端:
null 和 undefined 是无效的对象,因此是不会有 constructor 存在的,这两种类型的数据需要通过其他方式来判断。
函数的 constructor 是不稳定的,这个主要体现在把类的原型进行重写,在重写的过程中很有可能出现把之前的constructor给覆盖了,这样检测出来的结果就是不准确的
function Fn(){} Fn.prototype = new Array() var f = new Fn console.log(f.constructor)//Array
5.Object.prototype.toString.call()
Object.prototype.toString.call() 最准确最常用的方式。首先获取Object原型上的toString方法,让方法执行,让toString方法中的this指向第一个参数的值。
关于toString重要补充说明:
本意是转换为字符串,但是某些toString方法不仅仅是转换为字符串
对于Number、String,Boolean,Array,RegExp、Date、Function原型上的toString方法都是把当前的数据类型转换为字符串的类型(它们的作用仅仅是用来转换为字符串的)
Object上的toString并不是用来转换为字符串的。
Object上的toString它的作用是返回当前方法执行的主体(方法中的this)所属类的详细信息即"[object Object]",其中第一个object代表当前实例是对象数据类型的(这个是固定死的),第二个Object代表的是this所属的类是Object。
Object.prototype.toString.call('') ; // [object String] Object.prototype.toString.call(1) ; // [object Number] Object.prototype.toString.call(true) ; // [object Boolean] Object.prototype.toString.call(undefined) ; // [object Undefined] Object.prototype.toString.call(null) ; // [object Null] Object.prototype.toString.call(new Function()) ; // [object Function] Object.prototype.toString.call(new Date()) ; // [object Date] Object.prototype.toString.call([]) ; // [object Array] Object.prototype.toString.call(new RegExp()) ; // [object RegExp] Object.prototype.toString.call(new Error()) ; // [object Error] Object.prototype.toString.call(document) ; // [object HTMLDocument] Object.prototype.toString.call(window) ; //[object global] window是全局对象global的引用
The above is the detailed content of What are the data types of JavaScript? Introduction to js data types. For more information, please follow other related articles on the PHP Chinese website!

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

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.


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

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

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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