


Detailed explanation of basic data types and value type references in JavaScript
[Introduction] 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. This 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
Undefined A type has only one value, 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, which is 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 quotes. You can use single or double quotes:
##
function test3(){ var testMessage = "hello"; alert(typeof testMessage) }
4, number
can be a floating point number or 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) }
##
7、function
函数类型
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的值:Detailed explanation of basic data types and value type references in JavaScriptn" + objDemo1.url + "Detailed explanation of basic data types and value type references in JavaScriptn" + "objDemo2.url的值:Detailed explanation of basic data types and value type references in JavaScriptn" + 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 Detailed explanation of basic data types and value type references in JavaScript. 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

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

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

With the popularity of short video platforms, video matrix account marketing has become an emerging marketing method. By creating and managing multiple accounts on different platforms, businesses and individuals can achieve goals such as brand promotion, fan growth, and product sales. This article will discuss how to effectively use video matrix accounts and introduce different types of video matrix accounts. 1. How to create a video matrix account? To make a good video matrix account, you need to follow the following steps: First, you must clarify what the goal of your video matrix account is, whether it is for brand communication, fan growth or product sales. Having clear goals helps develop strategies accordingly. 2. Choose a platform: Choose an appropriate short video platform based on your target audience. The current mainstream short video platforms include Douyin, Kuaishou, Huoshan Video, etc.

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

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Go functions can return multiple values of different types. The return value type is specified in the function signature and returned through the return statement. For example, a function can return an integer and a string: funcgetDetails()(int,string). In practice, a function that calculates the area of a circle can return the area and an optional error: funccircleArea(radiusfloat64)(float64,error). Note: If the function signature does not specify a type, a null value is returned; it is recommended to use a return statement with an explicit type declaration to improve readability.

Introduction to JS-Torch JS-Torch is a deep learning JavaScript library whose syntax is very similar to PyTorch. It contains a fully functional tensor object (can be used with tracked gradients), deep learning layers and functions, and an automatic differentiation engine. JS-Torch is suitable for deep learning research in JavaScript and provides many convenient tools and functions to accelerate deep learning development. Image PyTorch is an open source deep learning framework developed and maintained by Meta's research team. It provides a rich set of tools and libraries for building and training neural network models. PyTorch is designed to be simple, flexible and easy to use, and its dynamic computation graph features make
