


Detailed explanation of javascript basic data types and how to perform conversion operations
There are 5 basic data types in
javascript: Undefined, Null, Boolean, Number, and String. There is also a complex data type - Object. Object is essentially composed of a set of unordered name-value pairs (key-value pairs). javascript Does not support any mechanism for creating custom types.
Since JavaScript is loosely typed, a means is needed to detect the data type of variables. Typeof is an operator with this function. Using typeof to detect variables may return one of the following strings:
"undefined" | Variable is undefined |
"boolean" | The variable is a Boolean value |
"string" | The variable is a string |
"number" | The variable is a numerical value |
"object" | The variable is an object or null |
"function" | Variables are functions |
From a technical point of view, a function is an object, not a data type. However, functions have some special properties, so it is necessary to use typeof to distinguish functions from other objects.
The Undefined type has only one value, which is the special undefined. When a variable is declared using var but not initialized, the value of the variable is undefined, such as:
var a; alert(a == undefined); //true
However, variables containing undefined values are different from undefined variables. For example:
var a; // 这个变量声明之后默认取得了undefined值 // 下面这个变量并没有声明 // var b alert(a); // "undefined" alert(b); // 产生错误
However, using typeof on undeclared or uninitialized variables will return undefined, such as:
var a; // var b; alert(typeof a); // "undefined" alert(typeof b); // "undefined"
The Null type also has only one value, which is null. From a logical point of view, the null value represents a null pointer, so using typeof to detect the null value will return "object", such as:
var car = null; alert(typeof car); // "object"
So if you want to define a variable To store objects, it is best to initialize the variable to null. In fact, the undefined value is inherited from the null value, so judging their equality will return true:
alert(null == undefined); // true
Although null and undefined have such a relationship, their uses are completely different, because there is no need to explicitly set the value of a variable to undefined at any time. However, when defining an object variable that has not yet saved the object, it is The variable should be set to null, so that it can not only reflect null as a pointer to an empty object, but also distinguish null and undefined.
The Boolean type has two literal values: true and false, but all types of values can be converted into Boolean type values by calling the Boolean() function. The following table lists the conversion rules corresponding to various data types. :
Data type | The value converted to true | The value converted to false |
Boolean | true | false |
String | Any non-empty string | ""empty String |
Number | Any non-zero numeric value | 0 and NaN |
Object | Any object | null |
Undefined | / | undefined |
Number类型分为整数和浮点数,整数可以用十进制,八进制或十六进制表示,如:
var num1 = 22; //十进制整数 var num2 = 070; //八进制的56 var num3 = 079; // 无效的八进制,解析为十进制79 var num4 = 08; //无效的八进制,解析为十进制8 var num5 = 0xA; //十六进制的10 var num6 = 0x1f; //十六进制的31
但是八进制字面量在严格模式下是无效的,在进行算数计算时,所有的数值最终都会转换为十进制数值。浮点数值必须包含一个小数点,如:
var floatNum1 = 1.1; var floatNum2 = .1; //有效,但不推荐 var floatNum3 = 1.; //小数点后面没有数字,解析为1 var floatNum4 = 10.0; //整数,解析为10
浮点数值的最高精度是17位小数,但在进行算数计算时精确度远不如整数,例如:
var a = 0.1; var b = 0.2; var c = a + b; //c的值为0.30000000000000004
NaN,即非数值,是一个特殊的Number值,NaN有两个特点:任何和NaN操作的结果都会返回NaN,NaN与任何值都不相等,包括NaN。使用isNaN()函数可以判断一个值是不是NaN,isNaN()在接收到一个参数时,会尝试将这个值转换为数值,任何不能转换为数值的值都会返回true,如:
alert(isNaN(NaN)); //true alert(isNaN(10)); //false(10是一个数值) alert(isNaN("10")); //false(可以被转换为数值10) alert(isNaN("abc")); //true(不能转换为数值) alert(isNaN(true)); //false(可以转换为数值1) var obj = {name:"zhangsan", age:"1"}; alert(isNaN(obj)); //true
isNaN()也能转换对象,对象调用isNaN()时,会首先调用对象的valueOf()方法,然后确定该方法的返回值是否可以转换为数值,如果不能,则用这个返回值再调用toString()方法,再测试返回值。
非数值转换成数值的方法有三个:Number()、parseInt()、parseFloat()。Number()可以转换任何数据类型的值,而parseInt()和parseFloat()只能转换字符串。
Number()函数有以下转换规则:
1.如果是Boolean值,true转换为1,false转换为0;
var num = Number(true); //1 var num2 = Number(false); //0
2.如果是Number值,就和传入的值一样;
var num = Number(1); //1
3.如果是null,转换为0;
var num = Number(null); //0
4.如果是undefined,转换为NaN;
var num = Number(undefined); //NaN
5.如果是String值,要分多种情况,如果是空字符串,则转换为0;如果是纯数字的字符串,则将其转换为相对应的数值,如果字符串是数字且包含".",则将其转换为对应的浮点数值(如果字符串最前面是0,会被忽略),如果字符串是有效的十六进制格式,会将其转换为对应的十进制数值;如果字符串包含上述格式之外的字符,则转换为NaN;如果字符串是对象,会首先调用对象的valueOf()方法,然后确定该方法的返回值是否可以转换为数值,如果结果是NaN,则调用toString()方法,再测试返回值。
var num = Number("Hello World"); //NaN var num2 = Number(""); //0 var num3 = Number("01"); //1 var num4 = Number("01.1"); //1.1 var obj = {name:"zhangsan", age:"1"}; alert(Number(obj)); //NaN
由于Number()在转换字符串是比较复杂,所以转换字符串常用parseInt()和parseFloat()。这两个函数在转换字符串时,会检测该字符串是否符合数值模式,从第一个非空格字符开始解析,如果第一个字符不是数值或者负号,则返回NaN(包括空字符串)。如果第一个字符是字符串,则继续解析后面的字符,直到解析完所有的字符或者遇到非数字字符。
parseInt()能够识别各种整数格式(十进制、八进制、十六进制),如果字符串以"0x"开头且后跟数字字符,就会被解析为十六进制,如果以"0"开头且后跟数字字符,则会被解析为八进制(ECMAScript5不识别八进制,会将前面的0忽略,解析为十进制)。
var num = parseInt("123Hello"); //123 var num2 = parseInt(""); //NaN var num3 = parseInt("0xA"); //10(十六进制) var num4 = parseInt("22.3"); //22 var num5 = parseInt("070"); //56(ECMAScript3八进制) 70(ECMAScript5十进制) var num6 = parseInt("23"); //23(十进制)
为了解决兼容性问题,parseInt()提供第二个参数,以何种数值格式解析。
var num1 = parseInt("0xAF", 16); //175 var num2 = parseInt("AF", 16); //175,可以省略前面的"0x" var num3 = parseInt("10", 2); //2(二进制) var num3 = parseInt("10", 8); //8(八进制) var num3 = parseInt("10", 10); //10(十进制) var num3 = parseInt("10", 16); //16(十六进制)
parseFloat()只识别第一个小数点,后面的小数点就无效了,同时parseFloat()只识别是十进制值,所以没有第二个参数,别的格式数值会被解析为0。
var num = parseFloat("123Hello"); //123 var num = parseFloat("0xA"); //0 var num = parseFloat("12.1"); //12.1 var num = parseFloat("12.1.1"); //12.1 var num = parseFloat("023"); //23 var num = parseFloat("1.1e3"); //1100
String类型值由若干个Unicode字符组成的字符序列构成,可以由单引号('')或者双引号("")表示,但是左右引号必须匹配。
var str1 = "abc"; var str2 = 'abc';<br>var str3 = "abc'; //语法错误
将一个值显式转换为字符串有两种方法,toString()和String(),数值、布尔值、对象和字符串都有toString()方法和String()方法,而undefined和null只有String()方法,toString()的参数是转换的进制格式。
var num = 10; alert(num.toString()); //"10" alert(num.toString(2)); //"1010" alert(num.toString(8)); //"12" alert(num.toString(10)); //"10" alert(num.toString(16)); //"A" alert(true.toString()); //"true" String(num); //"10" String(true); //"true" String(null); //"null" var num1; String(num1); //"undefined"
The above is the detailed content of Detailed explanation of javascript basic data types and how to perform conversion operations. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

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.

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a
