02-JS中的数据类型及类型转换(Data type and type conversion in 02-js)

02-JS中的数据类型及类型转换

一、数据类型

JS中的值,无论是字面量还是变量,都有明确的类型。

(一)概述

1.基本类型5种

number 数字类型

string 字符串类型

boolean 布尔类型,仅有两个值true和false

undefined undefined类型,此类型值只有一个,就是undefined

null null类型,这种类型的值也就只有一个null

2.引用类型

(二)数据类型的检测

使用typeof关键字检查数据类型,通常用来检测变量的类型,因为直接量的类型一眼就看的出来

var a = 123;
console.log(typeof a);  //number

1.number数字类型

//下面定义的变量都是number类型
<script>
    var a = 200;
    var b = -200;
    var c = 200.235;
    var d = .5e4;
    var e = 0xf0;
    var f = 016;
    var g = Infinity;
    var h = NaN;
    console.log(typeof a);
    console.log(typeof b);
    console.log(typeof c);
    console.log(typeof d);
    console.log(typeof e);
    console.log(typeof f);
    console.log(typeof g);
    console.log(typeof h);
  </script>

number类型的东西,所有的数字(不分正负、整浮、大小、进制、infinity、NaN)

2.string字符串类型

	var str1 = "小蔡啊";
    var str2 = "666";
    var str3 = "";  //空字符串也是字符串
    console.log(typeof str1);
    console.log(typeof str2);
    console.log(typeof str3);

3.boolean类型

boolean类型(布尔类型),布尔类型只有两个值true false,即真或假。

var bool = true;
console.log(bool );
console.log(typeof bool);  //boolean

注意:此时true和false没有加引导,所以不是字符串,同时注意它也是变量,true是一个关键字,本身就有特殊意义,表示真和假。

4.undefined类型

前面提到过,如果只var了一个变量,没有赋初值,它的默认值是undefined。

这个undefined是自成一家,类型和值都是undefined,这种类型的值也只有一个。

var un;  //这里只定义了,没有赋初值
console.log(un);
console.log(typeof un);   //undefined

(三)变量的类型

变量是什么类型,和赋值有关,而和定义的时候是没有关系的。定义的时候都是使用var关键字定义。

//动态数据类型
var num = 123;  //number
console.log(typeof num);
num = "哈哈"; //改为string是合法的
console.log(typeof num);

我们说JS这个语言叫做动态数据类型,就是赋值的时候就决定这个变量的类型了。甚至可以改变这个变量的值,为其他类型的值。变量的类型是自动检测,就是检查变量存储的值的类型,不是认为定义的。

二、运算符:加号 +

加号两边都是数字的时候,那么就是数学加法;

两边只要有一边是字符串,那么结果就是字符串拼接。

console.log("小" + "蔡");  //小蔡
console.log(3 + 2);  //5
console.log(3 + "2");  //32

多个加号,运算顺序是从左至右

<script>
    var a = 3;
    var b = 2;
    var c = 5;
    console.log( a + b + c);    //10
    console.log( a + b + 'c');   //5c
    console.log( 'a' + a + b + 'c');   //a32c
    console.log( 1 + 2 + "a" + "(3 + 4)");   //3a(3 + 4)
  </script>

运算从左到右计算,默认是数字加分,但是一旦遇到字符串就变成字符串拼接了

三、数据类型转换

JS中有一些方法可以将内存中表示其他数据类型转换为对应的数字类型。

因此,先认识一个语句prompt(),这个语句和alert差不多,也是弹窗,弹的是输入框

prompt("请输入你的电话","191");

这些小功能,就叫做程序给我们提供的API,每个API都有自己不同的语法。

prompt(“提示文本”,”默认值”);

默认值是可以省略的。

可以把用户输入的值,存入变量:

var a = prompt("请输入你的电话","191");
alert("你输入的电话是" + a);

用prompt接收的任何东西都是字符串,哪怕用户输入了一个数字,也是字符串的数字。

(一)转number

1.Number方法

(1) string “12px”=>NaN “”=>0

(2) boolean true=>1 false=>0

(3) null null=>0

(4) undefined undefined =>NaN

(5) {} {} => NaN

(6)[] [1]=>1, []=>0, [1,2]=>NaN

(7) function Number(function(){})=>NaN

例子:

// 计算器
    // 第一步让用户先输入第一个数字
    var a = prompt("请输入第一个数字");
    // 第二步让用户输入第二个数字
    var b = prompt("请输入第二个数字");
    // 第三步求和
    var sum = Number(a) + Number(b);
    // 第四步弹出结果
    alert(sum);

2.parselnt方法

parseInt就是将一个string转为一个整数,不四舍五入,直接截取整数部分。如果这个string有乱七八糟的东西,直接截取前面数字部分。

所以使用这个方法可能会丢失小数部分

var a = "678";
    var b = parseInt(a);  //parseInt就把字符串678转换为数字678
    console.log(b);   //678
    console.log(typeof b); //unmber

	parseInt("200"); //200
    parseInt("200.666");  //200
    parseInt("200小时");  //200
    parseInt("200年零2个月");  //200
    parseInt("200px");   //200
    parseInt("-200.9999");   //-200

parseInt()不仅能够转为整数,还可以进行进制的转换,把任何进制的数字,都换为10进制。

进制转换的字符串,用逗号隔开。

下面的运算结果都是15:

	parseInt(15,10); 
    parseInt(17,8); 
    parseInt(1111,2); 
    parseInt("0xf",16); 
    parseInt("f",16); 
    parseInt(16,9); 
    parseInt("15e6",10); 
    parseInt("15*6",10);

3.parseFloat方法

parseFloat就是将字符串转换为浮点数

尽可能的将一个字符串转为浮点数,浮点数之后如果有乱七八糟的内容,直接舍弃。

var a = "123.456.12";
    var b = parseFloat(a);
    console.log(b);   //123.456
    console.log(parseFloat("123.66年"));   //123.66
    console.log(parseFloat("你说123.66年"));  //NaN

也就是说,数字类型都是number,不分整数和浮点数,但是转换的时候分。

4.isNaN()方法

作用:判断一个数字是不是NaN

如果是,则结果是ture;如果不是,则结果是false

isNaN()方法的作用是判断数字是不是NaN,那它能判断其他数据类型吗?

isNaN(true) ==> false

isNaN(‘12.5px’) ==> true

isNaN(null) ==> false

isNaN(undefined) ==> true

结论:

isNaN()这个方法会先把其他类型数据转化成数字类型,之后判断是不是NaN

(二)转string

将一个数字,与一个空字符串进行连字符运算,那么就是自动转为字符串了。

var a = 123;
var b = a + "";
console.log(b);
console.log(typeof b);  //string

(三)转boolean

Boolean()方法

只有六种情况Boolean(被转换的数据)的结果是false,其余全是true。

1.0 数字0

2.NaN 数字NaN

3.”” 空字符串

4.false 布尔值false

5.undefined undefined类型

6.null null类型

用的不多了解几个。。。

————————

Data type and type conversion in 02-js

1、 Data type

Values in JS, whether literal or variable, have explicit types.

(1) Overview

1. 5 basic types

Number number type

String string type

Boolean boolean type, with only two values true and false

Undefined type. There is only one value of this type, which is undefined

Null type, the value of this type has only one null

2. Reference type

(2) Detection of data type

Using the typeof keyword to check the data type is usually used to detect the type of variable, because the type of direct quantity can be seen at a glance

var a = 123;
console.log(typeof a);  //number

1.number数字类型

//下面定义的变量都是number类型
<script>
    var a = 200;
    var b = -200;
    var c = 200.235;
    var d = .5e4;
    var e = 0xf0;
    var f = 016;
    var g = Infinity;
    var h = NaN;
    console.log(typeof a);
    console.log(typeof b);
    console.log(typeof c);
    console.log(typeof d);
    console.log(typeof e);
    console.log(typeof f);
    console.log(typeof g);
    console.log(typeof h);
  </script>

Number type things, all numbers (regardless of positive or negative, integer floating, size, base, infinity, Nan)

2. String string type

	var str1 = "小蔡啊";
    var str2 = "666";
    var str3 = "";  //空字符串也是字符串
    console.log(typeof str1);
    console.log(typeof str2);
    console.log(typeof str3);

3.boolean类型

Boolean type (boolean type). Boolean type has only two values true and false, that is, true or false.

var bool = true;
console.log(bool );
console.log(typeof bool);  //boolean

Note: at this time, true and false are not guided, so they are not strings. At the same time, note that they are also variables. True is a keyword, which has special meaning and represents true and false.

4.undefined类型

As mentioned earlier, if only one variable is assigned to var without an initial value, its default value is undefined.

This undefined is self-contained. Both types and values are undefined, and there is only one value of this type.

var un;  //这里只定义了,没有赋初值
console.log(un);
console.log(typeof un);   //undefined

(3) Type of variable

The type of variable is related to the assignment, but it has nothing to do with the definition. The VaR keyword is used for definition.

//动态数据类型
var num = 123;  //number
console.log(typeof num);
num = "哈哈"; //改为string是合法的
console.log(typeof num);

We say that JS language is called dynamic data type, which determines the type of this variable when assigning values. You can even change the value of this variable to other types of values. The type of variable is automatically detected, which is to check the type of value stored in the variable, not defined.

2、 Operators: plus sign+

When both sides of the plus sign are numbers, it is mathematical addition;

As long as one side is a string, the result is string splicing.

console.log("小" + "蔡");  //小蔡
console.log(3 + 2);  //5
console.log(3 + "2");  //32

Multiple plus signs, the operation order is from left to right

<script>
    var a = 3;
    var b = 2;
    var c = 5;
    console.log( a + b + c);    //10
    console.log( a + b + 'c');   //5c
    console.log( 'a' + a + b + 'c');   //a32c
    console.log( 1 + 2 + "a" + "(3 + 4)");   //3a(3 + 4)
  </script>

The operation is calculated from left to right. The default is digital plus points, but once a string is encountered, it becomes string splicing

3、 Data type conversion

JS has some methods that can convert other data types represented in memory to corresponding numeric types.

Therefore, first recognize a statement prompt (), which is similar to alert. It is also a pop-up window, which pops up the input box

prompt("请输入你的电话","191");

These small functions are called the APIs provided by the program. Each API has its own different syntax.

Prompt (“prompt text”, “default”);

The default value can be omitted.

The value entered by the user can be stored in the variable:

var a = prompt("请输入你的电话","191");
alert("你输入的电话是" + a);

Anything received with prompt is a string. Even if the user enters a number, it is also a number of the string.

(一)转number

1.Number方法

(1) string “12px”=>NaN “”=>0

(2) boolean true=>1 false=>0

(3) null null=>0

(4) undefined undefined =>NaN

(5) {} {} => NaN

(6)[] [1]=> 1, []=> 0, [1,2]=> NaN

(7) function Number(function(){})=>NaN

example:

// 计算器
    // 第一步让用户先输入第一个数字
    var a = prompt("请输入第一个数字");
    // 第二步让用户输入第二个数字
    var b = prompt("请输入第二个数字");
    // 第三步求和
    var sum = Number(a) + Number(b);
    // 第四步弹出结果
    alert(sum);

2.parselnt方法

ParseInt is to convert a string into an integer without rounding, and directly intercept the integer part. If there is something messy in this string, directly intercept the previous number part.

Therefore, using this method may lose the decimal part

var a = "678";
    var b = parseInt(a);  //parseInt就把字符串678转换为数字678
    console.log(b);   //678
    console.log(typeof b); //unmber

	parseInt("200"); //200
    parseInt("200.666");  //200
    parseInt("200小时");  //200
    parseInt("200年零2个月");  //200
    parseInt("200px");   //200
    parseInt("-200.9999");   //-200

Parseint() can not only be converted into an integer, but also be converted into hexadecimal. Any hexadecimal number can be changed into hexadecimal.

Hexadecimal converted strings, separated by commas.

The following calculation results are 15:

	parseInt(15,10); 
    parseInt(17,8); 
    parseInt(1111,2); 
    parseInt("0xf",16); 
    parseInt("f",16); 
    parseInt(16,9); 
    parseInt("15e6",10); 
    parseInt("15*6",10);

3.parseFloat方法

Parsefloat is to convert a string to a floating point number

Try to convert a string into a floating-point number. If there is any mess after the floating-point number, it will be discarded directly.

var a = "123.456.12";
    var b = parseFloat(a);
    console.log(b);   //123.456
    console.log(parseFloat("123.66年"));   //123.66
    console.log(parseFloat("你说123.66年"));  //NaN

In other words, the number type is number, regardless of integer and floating-point number, but the conversion time is minute.

4. IsNaN () method

Function: judge whether a number is Nan

If yes, the result is true; If not, the result is false

The function of IsNaN () method is to judge whether the number is Nan. Can it judge other data types?

isNaN(true) ==> false

isNaN(‘12.5px’) ==> true

isNaN(null) ==> false

isNaN(undefined) ==> true

Conclusion:

The IsNaN () method will first convert other types of data into digital types, and then judge whether it is Nan or not

(二)转string

If a number is hyphenated with an empty string, it will be automatically converted to a string.

var a = 123;
var b = a + "";
console.log(b);
console.log(typeof b);  //string

(三)转boolean

Boolean() method

In only six cases, the result of Boolean (converted data) is false, and the rest are true.

1.0 digit 0

2. Nan digital Nan

3. “” empty string

4.false Boolean false

5.undefined undefined类型

6.null null类型

I don’t know much about it…