Table of Contents
二、值类型与引用类型" >二、值类型与引用类型
Home Web Front-end JS Tutorial Detailed explanation of basic data types and value type references in JavaScript

Detailed explanation of basic data types and value type references in JavaScript

Aug 10, 2017 pm 12:01 PM
javascript js type

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

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);
}
Copy after login

Detailed explanation of basic data types and value type references in JavaScript

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);
}
Copy after login

Detailed explanation of basic data types and value type references in JavaScript

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

Detailed explanation of basic data types and value type references in JavaScript

4, number

can be a floating point number or an integer

function test4(){
var testMessage = 12;
alert(typeof testMessage)
}
Copy after login

Detailed explanation of basic data types and value type references in JavaScript

5, boolean

##Boolean type, has two values: true or false.

function test5(){
var testMessage = true;
alert(typeof testMessage)
}
Copy after login

Detailed explanation of basic data types and value type references in JavaScript

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

Detailed explanation of basic data types and value type references in JavaScript

Detailed explanation of basic data types and value type references in JavaScript

Detailed explanation of basic data types and value type references in JavaScript

##

7、function

函数类型

function test9(){
alert(typeof test8)
}
Copy after login

Detailed explanation of basic data types and value type references in JavaScript

二、值类型与引用类型

(1)值类型:数值、布尔值、null、undefined

值类型指的是保存在栈内存中的简单数据段,按值访问,操作的是他们实际保存的值;

(2)引用类型:对象、数组、函数

引用类型指的是那些保存在堆内存中的对象,意思是,变量中保存的实际上只是一个指针,这个指针执行内存中的另一个位置,由该位置保存对象;引用访问,当查询时,我们需要先从栈中读取内存地址,然后再顺藤摸瓜地找到保存在堆内存中的值;

如:以下都是引用类型

var cars=   new Array;
var person= new Object;
Copy after login

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

Detailed explanation of basic data types and value type references in JavaScript

Detailed explanation of basic data types and value type references in JavaScript

Detailed explanation of basic data types and value type references in JavaScript

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

Detailed explanation of basic data types and value type references in JavaScript

 

Detailed explanation of basic data types and value type references in JavaScript

Detailed explanation of basic data types and value type references in JavaScript

注意:
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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

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

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

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

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

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

How to create a video matrix account? What types of matrix accounts do it have? How to create a video matrix account? What types of matrix accounts do it have? Mar 21, 2024 pm 04:57 PM

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.

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

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 The relationship between js and vue Mar 11, 2024 pm 05:21 PM

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.

What is the type of return value of Golang function? What is the type of return value of Golang function? Apr 13, 2024 pm 05:42 PM

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.

The AI ​​era of JS is here! The AI ​​era of JS is here! Apr 08, 2024 am 09:10 AM

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

See all articles