content
2. Template strings (new in ES6), - template literals
1. Data Type
1.1 Why data types are needed
Because the storage space occupied by different data is different, in order to facilitate dividing the data into data with different memory sizes, the storage space is fully utilized.
1.2 Data Types of Variables
JS is a weakly typed or dynamic language that automatically determines the type during runtime.
var x = 123 ;
var x = 'syh'; // type can vary
1.3 Data Type Classification
1. Simple data types
2. Complex data types
simple data type | illustrate | Defaults |
Number | digital, 21, 0.21 | 0 |
Boolean | Boolean value types such as true, false | fales 0 ture 1 |
String | String type ' ' | “ ” |
undefined | var a; undefined | undefined |
Null | var a=NULL; a is a null value | null |
Notice:
//1.八进制
var num=010;// 前面第一位0表示8进制
console.log(num);
//2.十六进制
var num1=0x9;// 0x表示16进制
console.log(num);
//3.数字型Number 最大值 最小值
alert(Number.MAX_VALUE);
alert(Number.MIN_VALUE);
//三个特殊值
alert(Number.MAX_VALUE*2);//Infinity
alert(Number.MIN_VALUE*2);//-Infinity
alert('pink老师'-100);//NAN 非数值 如果是数字返回false 如果不是返回ture
//isNAN() 判断非数字
// console.log(is NAN(21)); 返回false
//4.字符串型 string
var str="我是一个'数学不好'的\n程序员";
console.log(str);//我是一个'数学不好'的
//程序员
//字符串转义 字符要写到引号中
// \'单引号 \"双引号 \t tab \b 空格
//求字符串长度
var str="my name is andy";
console.log(str.length);//15
//字符串拼接
console.log('沙漠'+'骆驼');//沙漠骆驼
console.log('pink'+18);//pink
console.log('pink'+ture);//pinkture
console.log(12+12);//24
console.log('12'+12);//'1212'
//只要有字符串和其他类型拼接结果都是字符串类型
//6.undefined和NULL
var variable;
console.log(variable);//undefined
console.log('你好'+variable);//你好variable
console.log(11+variable);//NaN
console.log(ture+variable);//NaN
var variable=null;
console.log('你好'+variable);//你好null
console.log(11+variable);//11
console.log(ture+variable);//1
2. Template strings (new in ES6), - template literals
2.1 Parsing variables
let name='张三';
let sayHello='Hello,我的名字叫${name}';
console.log(sayHello);
2.2 Strings can wrap
let result ={
name:"ZHANGSAN",
age:20
};
let html =
<div>
<span>${result.name}</span>
<span>${result.age}</span>
</div>
;
console.log(html);
2.3 Calling the function
const fn=()=>{
return '我是fn函数'
}
let html ='我是模板字符串${fn()}'
console.log(html);
3.typeof and literals
3.1 Use of typeof
expand:
prompt takes the character type
3.2 Literals
A literal is a representation of a fixed value in source code
Numeric literals: 8,9,10
String literal: 'Black Friday'
Boolean literal: true false
4. Data type conversion
4.1 What is a data type
Using the form (intput), the data obtained by the prompt is a string type by default. At this time, the addition operation cannot be performed directly, and character conversion is required, which is to convert a variable of one data type into another data type.
There are usually three types
1) Convert to string type
2) Convert to numeric
3) Convert to boolean
4.2 Convert to string type
Way | illustrate |
variable tostring() | convert to string |
string coercion | convert to string |
Plus sign concatenates strings | The result of concatenating with strings is a string |
4.3 Convert to digital
Way | illustrate | case |
parseInt(string) function | convert string to numeric | parseInt('78')//78 |
parseFloat(string) function | convert string to float | parseFloat ('78 .82 ') // 78.82 |
Number() cast | convert string to numeric | Number(‘12’)//12 |
Implicit conversion (-+/-) | Implicit conversion to numeric type using arithmetic operations | ‘12’-0 |
4.4 Convert to Boolean
Way | illustrate | case |
Boolean() function | Convert other types to Boolean | Boolean(‘1’)//ture |