JavaScript ----- 4. Data Type

1. variable data

Place where the variable is used to store value, has a name and data type. The data type of the variable to determine how these values ​​bit memory into the computer's memory. JavaScript is a weakly typed or dynamic languages. Do not declare in advance the type of variable, the variable will be automatically determined in the program run.

var age;//不确定类型
age = 10;//确定位整型

The java \ C is

int age = 10;//直接确定为整形

python can not declare

age = 10;

JavaScript has dynamic typing, but also means that the same variables can be used for different types of

var x = 6;//x为数字
var x = "Bill"; //x为字符串

2. The type of data classification

JS divided data types: simple data types (Number, String, Boolean, Underfined, Null) and complex data types (object)

2.1 simple data types

2.1.1 Number: numeric values ​​and shaping comprises a floating-point value, the default value is 0

(1) hex

var num = 10; //num数字型
var PI = 3.14;//PI数字型

//八进制:0~7程序里数字前加0表示8进制数字
var num1 = 010;
console.log(num1);//打印输出的是8
var num2 = 012;
console.log(num2);//打印输出10

//十六进制:0~9 a~f程序里数字前加0x表示16进制数字
var num3 = 0x9;
console.log(num3);//打印输出的是9
var num4 = 0x12;
console.log(num4);//打印输出的是18

(2) a numeric range of
the maximum values in JavaScript (Number.MAX_VALUE) and the minimum value (Number.MIN_VALUE)
Number.MAX_VALUE: 1.7976931348623157e + 308
Number.MIN_VALUE: 5E-324
Inifit: infinity, any value greater than
-Infint: infinitesimal, less than any value
NaN: not a number, representing a non-numeric (e.g. subtraction and a string an integer)

(3) isNaN ()
for determining whether a variable is non-numeric type, the value is returned if false, returns true if the non-numeric

//isNaN()用这个方法用来判断非数字,并且返回一个值,如果是数字则返回false否则返回true
console.log(isNaN(12));//false
console.log(isNaN('pink 老师'))//true

2.1.2 String: string type, the default value is an empty string ""

(1) may be a single quotation marks may be double quotes, JS recommended by single quotes (html by double quotation marks)
(2) escape character
\ n-: newline
\: slash ': single quote'
": bis quotes "
\ t: the Tab indent
\ b: space (b meaning is blank)

(3) the length of the string and the splice
length attribute used to obtain the length of the string:

//检测字符串长度length
var str = 'my name is andy';
console.log(str.length);

Using the '+' splicing string formulas: adding value, bridged characters:

console.log('沙漠'+'骆驼');//沙漠骆驼

//字符型和其他类型进行拼接最后的结果还是字符串型的
console.log('pink老师'+18);//pink老师18
console.log(18+'pink老师');//18pink老师
console.log(true+'pink');//truepink
console.log('12'+12);//1212
//但是以下就不是拼接
console.log(12+12);//24

String concatenation strengthen:

var age = 18;
console.log('pink老师'+age+'岁');

Case:

//弹出一个输入框,需要用户输入年龄,之后弹出一个警示框显示“您今年xx岁啦”
age = prompt('请输入年龄');
alert('您今年岁'+age+'啦');

2.1.3 Boolean: Boolean value types such as true, false, false defaults

var flag = true;
var flag1 = false;
console.log(flag+1);//2
console.log(flag1+1);//1

2.1.4 Undefined

var a; but there is no statement to the variable value, then a = underfined, Default underfined

//如果一个变量声明但是未赋值 就是underfined未定义数据类型
var str;
console.log(str);//undefined
var variable = undefined;
console.log(variable);//undefined
console.log(variable+'pink');//undefinedpink     字符串类型
console.log(variable+1);//NaN  undefined和数字相加最后的结果是NaN
console.log(variable+true);//NaN

2.1.5 Null:

var a = null; declares a variable to a null value

//null
var space = null;
console.log(space);
console.log(space+'pink');//nullpink
console.log(space+1);//1

3. Get the data type of the variable

3.1 typeof

typeof be used to obtain the data type detection variable

var num = 10;
console.log(typeof num);//number
var str = 'pink';
console.log(typeof str);//string
var flag = true;
console.log(typeof flag);//boolean
var vari = undefined;
console.log(typeof vari);//undefined
var timer = null;
console.log(typeof timer);//object

Prompt type using typeof verification value is taken over

age = prompt('请输入您的年龄');
console.log(age);//18
console.log(typeof age);//string

3.2 console

In addition to be detected with typeof variable which belongs to the type may also be judged by the color of the console console browser type is data which

console.log(18);//数字型 纯蓝色
console.log('18');//字符型 黑色
console.log(true);//布尔型 墨蓝色
console.log(undefined);//浅灰色
console.log(null);//浅灰色

3.3 literals

Literal representation of the source code is a fixed value, it is popular literal expression value showing how
digital literal: 8,9,10
string literal: 'horse Programmers', 'large front end'
Boolean literal: true, false

4. Data type conversion

4.1 What is a data type conversion

Using forms (input), prompt data acquired over the default string type, then it can not easily be directly adding, the need to convert the data type of the variable, it is popular to a data type of the variable to be converted into another data types.
3 ways of common conversion:

  • Converted to a string
  • Converted to a digital type
  • Converted to a boolean

4.1.1 into a string type

var num = 10;

//1. 把数字型转换成字符串  格式:变量.toString()
var str = num.toString();
console.log(str);
console.log(typeof str);
//2. 我们利用String()   格式:String(变量)
console.log(String(num));
//3. 利用+拼接字符串的方法实现转换效果  隐式转换
console.log(num+'');

4.1.2 converted to a digital type (Key)

(1) Method 1: parseInt (variable)

var age = prompt('请输入您的年龄');
//1. parseInt(变量) 可以把字符型的转化为数字型 得到的是整数(直接把小数点后面的数字抹掉)
console.log(parseInt(age));
console.log(parseInt('18.9')); //18
console.log(parseInt('120px')); //120 会直接将后面的字母去掉
console.log(parseInt('rem120px'));//NaN

(2) Method 2: parseFloat (variable)

//2. parseFloat(变量)可以把字符型转化为数字型 得到的是小数 浮点数
console.log(parseFloat('3.14'));
console.log(parseFloat('120px')); //120 会直接将后面的字母去掉
console.log(parseFloat('rem120px'));//NaN

(3) Method 3: Number (variable)

//3. 利用Number(变量)
var str = '123';
console.log(Number(str));
console.log(Number('12'));

(4) using the arithmetic - * / implicit conversion

//4. 利用了算数运算-*/隐式转换
console.log('12'-0);//12  若中间是加号则'120'
console.log('123'-'120');//3 若是加号则是'123120'
console.log('123'*1);//'123'
console.log('123'/1);//'123'

Guess you like

Origin www.cnblogs.com/deer-cen/p/11978679.html