Home > Web Front-end > JS Tutorial > JavaScript data types and their conversion analysis

JavaScript data types and their conversion analysis

黄舟
Release: 2017-03-15 14:47:01
Original
1208 people have browsed it

Although JavaScript is an interpreted programming language, there are not many data types, but as a web developer, some basic JavaScript data types still need to be mastered. This article mainly introduces all data types in JavaScript and the conversion between them.

JavaScript data type

1.Boolean (Boolean)

Boolean: (value type) var b1=true;//Boolean type

2.Number (number)

Number: (value type) var n1=3.1415926;//Number type

n1.toFixed(3);//Round to 3 decimal places.

3.String(string)

var s1=‘hello';//字符串类型
Copy after login

String: (value type, string immutable feature)

4.Undefined (undefined)

undefined belongs to the value type. The result calculated from other values ​​is not what we want, but it is slightly different from null in the database, such as Calculate numbers or evaluate results with strings.

Undefined type and Null type are data types with only one value, respectively undefined and null.

5.Null (empty object)

6.Object(Object type)

Object is a reference type, and others are basic data types.

String is also a basic type. You cannot add dynamic attributes to String, but you can when using reference types.

Reference type object instanceof type, determine whether a certain value is a certain type, all reference types instanceof Object returns true

7. Application type

Object (object ): (Reference type)

var tim=new Date();//对象类型(object)
var names=[‘zs','ls','ww'];//数组也是对象类型(object)
var obj=null;//object
Copy after login

Function : (Reference type)

function fun(){  }  //typeof(fun);//输出结果为function,函数类型
Copy after login

PS: Use typeof(variable)# to view the type of variable

##Null and undefined in JavaScript

undefined represents an unknown

state

The variable is declared but not initialized, and the value of the variable is unknown status(undefined). (Accessing non-existent properties or objects window.xxx) When the method does not explicitly return a value, the return value is an undefined. When the typeof

operator is applied to an undeclared variable, it is displayed as undefined (*)

null represents an object that does not yet exist. Null is a value with special meaning.

You can assign null to a variable. At this time, the value of the variable is "known state" (not undefined), that is, null. (Used to initialize variables, clear variable contents, and release memory)

undefined==null //The result is true, but the meaning is different.

undefined===null //false(*),PS: First determine whether the types are consistent, and then determine the value. ===Strictly equal, !==Strictly not equal

Because == will convert the value type before judging whether it is equal, sometimes there may be unexpected results, so it is recommended to use ===. But note that in some cases using == can bring better results.

Type conversion
parseInt(arg)将指定的字符串,转换成整数
parseFloat(arg)将指定的字符串,转换成浮点数
Number(arg)把给定的值(任意类型)转换成数字(可以是整数或浮点数);转换的是整个值,而不是部分值。如果该字符串不能完全转换为整型,则返回NaN。(Not a Number)
isNaN(arg),判断arg是否为一个非数字(NaN),NaN与NaN也不相等。
String(arg)把给定的值(任意类型)转换成字符串;
Boolean(arg)把给定的值(任意类型)转换成 Boolean 型;
(*)eval(codeString)将一段字符串的js代码,计算并执行。
Copy after login

The above is the detailed content of JavaScript data types and their conversion analysis. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template