


This article mainly talks about the basic data types in JavaScript, as well as the difference and use of value types and reference types
1. Basic data types
The keywords used to declare variables in JavaScript are all var, which is different from other programming languages. However, JavaScript also contains five basic data types (which can also be said to be simple data types). They are: Undefined, Null, Boolean, Number and String. It also contains a complex data type—Object.
(1), "undefined" - not declared, or the value of the variable is undefined or uninitialized;
(2) , "boolean" - if the value of this variable is of Boolean type;
(3), "string" - the value is of string type;
(4), "number" - the value is of numeric type;
(5), "object" - the object or value is null;
The keyword typeof must be mentioned, because JavaScript is loosely typed and does not use the corresponding type when declaring variables. keyword, if you want to know the basic data amount of a certain variable in the code, you can use typeof. What should be noted here is that typeof returns a string type.
(5), "function" - function.
Instance verification:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript"> function test1(){ var testMessage; alert(typeof testMessage); } function test2(){ var testMessage = null; alert(typeof testMessage); } function test3(){ var testMessage = "hello"; alert(typeof testMessage) } function test4(){ var testMessage = 12; alert(typeof testMessage) } function test5(){ var testMessage = true; alert(typeof testMessage) } function test6(){ var testMessage = []; alert(typeof testMessage) } function test7(){ var testMessage = []; alert(typeof testMessage) } function test8(){ var testMessage = new Object(); alert(typeof testMessage) } function test9(){ alert(typeof test8) } </script> </head> <body> <button type="button" id="button1" onclick = "test1()">测试undefined</button> <button type="button" id="button2" onclick = "test2()">测试null</button> <button type="button" id="button3" onclick = "test3()">测试string</button> <button type="button" id="button4" onclick = "test4()">测试number</button> <button type="button" id="button5" onclick = "test5()">测试boolean</button> <button type="button" id="button6" onclick = "test6()">测试[]</button> <button type="button" id="button7" onclick = "test7()">测试{}</button> <button type="button" id="button8" onclick = "test8()">测试Object</button> <button type="button" id="button9" onclick = "test9()">测试function</button> </body> </html>
1. Undefined
The Undefined type has only one value, that is undefined. When the declared variable has not been initialized, the default value of the variable is undefined
function test1(){ var testMessage; alert(typeof testMessage); }
##2, Null
The Null type also has only one value, null. null is used to represent an object that does not yet exist. It is often used to indicate that a function attempts to return a non-existent object
function test2(){ var testMessage = null; alert(typeof testMessage); }
3, string
String, the string can be any text in quotation marks. You can use single or double quotes:
function test3(){ var testMessage = "hello"; alert(typeof testMessage) }
can be a floating point number, an integer function test4(){
var testMessage = 12;
alert(typeof testMessage)
}
##5, boolean
Boolean type, has two values true or false.
function test5(){
var testMessage = true;
alert(typeof testMessage)
}
##6. obeject:
Objects and arrays, as well as null.
Objects and arrays can contain different types, including objects and arrays.function test6(){
var testMessage = [];
alert(typeof testMessage)
}
function test7(){
var testMessage = [];
alert(typeof testMessage)
}
function test8(){
var testMessage = new Object();
alert(typeof testMessage)
}
函数类型
function test9(){ alert(typeof test8) }
二、值类型与引用类型
(1)值类型:数值、布尔值、null、undefined
值类型指的是保存在栈内存中的简单数据段,按值访问,操作的是他们实际保存的值;
(2)引用类型:对象、数组、函数
引用类型指的是那些保存在堆内存中的对象,意思是,变量中保存的实际上只是一个指针,这个指针执行内存中的另一个位置,由该位置保存对象;引用访问,当查询时,我们需要先从栈中读取内存地址,然后再顺藤摸瓜地找到保存在堆内存中的值;
如:以下都是引用类型
var cars= new Array; var person= new Object;
1、值类型实例:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript"> function fun1(){ var a=1; var b=a; b=-1; alert("a="+a+" b="+b); } function fun2(){ var a=new String("lin"); var b=a; b = new String("bing"); alert("a="+a+" b="+b); } function fun3(){ var a="lin"; var b=a; b = "bing"; alert("a="+a+" b="+b); } </script> </head> <body> <button type="button" id="button1" onclick = "fun1()">测试值类型</button> <button type="button" id="button2" onclick = "fun2()">测试值类型</button> <button type="button" id="button1" onclick = "fun3()">测试值类型</button> </body> </html>
2、引用类型实例
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript"> function fun1(){ var a=[1,2,3]; var b=a; a[0]=1000; alert("a="+a+" b="+b); } function fun2(){ var a = [1,2,3]; var b = a; b = [11, 12, 13];//b指向了另一个内存地址,与a断开关联 a[0] = 2; alert("a="+a+" b="+b); } function fun3(){ function ClassDemo(){ this.name = "linbingwen"; this.url = "我的博客:http://blog.csdn.net/evankaka"; } var objDemo = new ClassDemo(); var objDemo1 = objDemo; var objDemo2 = objDemo; objDemo1.url = "我的主页:http://my.csdn.net/Evankaka"; alert( "objDemo1.url的值:Introduction to javascript basic data types and value type reference typesn" + objDemo1.url + "Introduction to javascript basic data types and value type reference typesn" + "objDemo2.url的值:Introduction to javascript basic data types and value type reference typesn" + objDemo2.url ); } </script> </head> <body> <button type="button" id="button1" onclick = "fun1()">测试引用类型</button> <button type="button" id="button2" onclick = "fun2()">测试引用类型</button> <button type="button" id="button3" onclick = "fun3()">测试引用类型</button> </body> </html>
注意:
undefined,null,空字符串,0都等于false,都可以通过!来取反。
The above is the detailed content of Introduction to javascript basic data types and value type reference types. For more information, please follow other related articles on the PHP Chinese website!

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

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.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor
