Home > Web Front-end > JS Tutorial > A brief discussion on JavaScript data types and conversion_Basic knowledge

A brief discussion on JavaScript data types and conversion_Basic knowledge

WBOY
Release: 2016-05-16 16:12:18
Original
1162 people have browsed it

JavaScript data types

1.Boolean

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

2.Number (number)

Numerical value: (value type) var n1=3.1415926;//Numeric type

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

3.String (string)

Copy code The code is as follows:

var s1=‘hello’;//String type

String: (value type, immutable character of string)

4.Undefined

undefined belongs to the value type, and the result obtained by calculation with other values ​​is not what we want, but it is slightly different from null in the database, such as the result of calculation with numbers or calculation 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 add reference types.

Reference type object instanceof type is used to determine whether a certain value is of a certain type. All reference types instanceof Object returns true

7.Application Type

Object: (reference type)

Copy code The code is as follows:

var tim=new Date();//Object type (object)
var names=['zs','ls','ww'];//Arrays are also object types (object)
var obj=null;//object

Function: (reference type)

Copy code The code is as follows:

function fun(){ } //typeof(fun);//The output result is function, function type

PS: To check the type of a variable, use typeof(variable)

Null and undefined in JavaScript

undefined, indicating an unknown state

If the variable is declared but not initialized, the value of the variable is in an unknown state (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

Copy code The code is as follows:

parseInt(arg) converts the specified string into an integer
parseFloat(arg) converts the specified string into a floating point number
Number(arg) converts the given value (any type) into a number (can be an integer or a floating point number); the entire value is converted, not part of the value. If the string cannot be completely converted to an integer, NaN is returned. (Not a Number)
isNaN(arg), determines whether arg is a non-number (NaN), NaN and NaN are not equal.
String(arg) converts the given value (any type) into a string;
Boolean(arg) converts the given value (any type) into Boolean type;
(*)eval(codeString) calculates and executes the js code of a string.

The above is the data type and conversion method of JavaScript. I hope you will like it.

Related labels:
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