[Note: JS] basic data types and data type conversion

Data type refers to the type of variable, in JS a total of six types of data:
. 1, String String
2, Number value
3, Boolean Boolean value
4, Null null
5, Undefined Undefined
6, Object Object

  • Wherein String, Number, Boolean, Null, Undefined belong to the basic data types, data types and references belonging to Object

1, String String type

  • Precautions:
  • JS quotes required to cause the string;
  • Use double or single quotes can be, but not mixed with use;
  • It can not be nested quotes, double quotes not put double quotes, single quotes not put single quotes;

Declare variables

var str = "hello"
var str1 = '我说:"今天天气真不错!"'

Two or more types of variables are declared String string type.

1, we can use the string \ as an escape character;
2, using the detection output typeof String ;

Escape character Example:

\"   表示输出 "
\'   表示输出 '
\n   表示输出一个换行
\t   表示一个制表符
\\   表示输出 \

2, Number Value Type

  • All the values ​​are JS Number types include integer (int) and floating point (a float), it is a decimal.
  • 1, JS digital representation may be:
    maximum: Number.MAX_VALUE: 1.7976931348623157e + 308
    greater than a minimum value of 0: Number.MIN_VALUE: 5e-324
  • 2, Number If digital representation exceeds the maximum, it is returned Infinity
    Infinity : Infinity represents positive infinity;
    -Infinity : -Infinity representing negative infinity;
    use typeof Infinity also returned check number.
  • . 3, NaN is a special number, indicates Not A Number, using typeof NaN also returns a check number.

Declare variables

var num = 123
var nbr =Infinity
var nmb = NaN

The above variables are declared as Number Value Type

1, calculates integer JS using basic guarantees correctness;
2, if the floating-point operations using JS, may obtain an inaccurate result;
3, the detection output of typeof Number The ;

3, Boolean type

Only two Boolean values, mainly used for logical decision:
. 1, to true: - indicates true;
2, to false: - for false.

var bool = false;
console.log(typeof bool);

When using typeof check a Boolean value, it will return boolean

4, Null (null)

Null value of only one type is null

  • The null value is used to represent a special null object
var a = null;
console.log(typeof a);

Typeof check using a null value is returned Object;

5, Undefined (undefined)

Undefined value of only one type, undefind

  • When you declare a variable, but not when assigned to a variable, its value is undefined
var b = undefined;
console.log(typeof b);

Also returns undefined undefined when using a check typeof

6, data type conversion

Cast: cast one data type to another data type.

6.1, the other type is converted to a string type String

Method in two ways: 1, tostring () method; 2, String () function

tostring () method: does not affect the original variables, the results of which will convert the return, so there should be a variable to hold the returned results;

var a = 123;
a = a.toString();
a = true;
a = a.toString();
a = null;
//a = a.toString();   //会报错
a = undefined;
//a = a.toString();   //会报错

Note: null and undefined these two values ​​do not toString () method, if you call their approach will complain

String () function: call String () function, and the converted data is passed to the function using the String () function as a parameter. When do cast for toString () method Number and Boolean is actually called, but for the null and undefined, it will not call the toString () method, it will directly convert null "null", will directly convert undefined is "undefined".

a = 123;
//调用String()函数,来将a转换为字符串
a = String(a);
a = null;
a = String(a);
a = undefined;
a = String(a);
console.log(typeof a);
console.log(a);
6.2, to convert other types of value type Number

Three kinds of converting mode: 1, Number () function; 2, parseInt () function; 3, parseFloat () function.

Number () function: string -> Digital

  • 1, if it is purely digital string directly converted to digital;
  • 2, if the content of non-numeric string, is converted into NaN3;
  • 3, if the string is a null string is a space or a full string, then converted to 0.
var a = "123";
//调用Number()函数来将a转换为Number类型
a = Number(a);
console.log(typeof a);
console.log(a);

Number () function: Istanbul -> Digital

  • 1, true converted into 1
  • 2, false turn into 0
a = false;
//调用Number()函数来将a转换为Number类型
a = Number(a);
console.log(typeof a);
console.log(a);

Number () function: null -> Digital 0

a = null;
//调用Number()函数来将a转换为Number类型
a = Number(a);
console.log(typeof a);
console.log(a);

Number () function: undefined-> Digital NaN

a = undefined;
//调用Number()函数来将a转换为Number类型
a = Number(a);
console.log(typeof a);
console.log(a);

the parseInt () function: the revolution of a string of significant figures after the change is taken out as an integer, the parseInt () function can have two values, the first parameter is a need to convert the content, and the second numerical value is converted to prepare the hex.

a = "124px";
//调用parseInt()函数来将a转换为Number类型
a = parseInt(a,10); //转换为十进制的数据类型
console.log(typeof a);
console.log(a);

: parseFloat () function is replaced after a decimal string can obtain a significant fraction taken out

a = "123.456.789px";
//调用parseFloat()函数来将a转换为Number类型
a = parseFloat(a);
console.log(typeof a);
console.log(a);

If the use of non-String the parseInt () or parseFloat (), it will first convert String then in operation

a = true;
a = parseInt(a);
console.log(typeof a);
console.log(a);

The previous output is NaN

6.3 to convert other type Boolean type

Only one conversion mode: Boolean () function

  • 1, a digital -> Boolean: In addition to 0 and NaN, the rest are true;
var a = 123;    //true
a = -123;       //true
a = 0;          //false
a = Infinity;   //true
a = NaN;        //false
a=Boolean(a)
console.log(typeof a);
console.log(a);
  • 2, String -> Boolean: In addition to the empty string, the rest are true
var a = "";
a=Boolean(a)
console.log(typeof a);
console.log(a);
  • 3, null and undefined -> Boolean: will be converted to false
var a =null;
a=Boolean(a)
console.log(typeof a);
console.log(a);
  • 4, Object -> Boolean: converted to true
var a ={};
a=Boolean(a)
console.log(typeof a);
console.log(a);
Published 20 original articles · won praise 11 · views 1752

Guess you like

Origin blog.csdn.net/qq_16221009/article/details/102825068