Article Directory
First, the basic concept of data types
- Data type : refers to the type literal, in JS, a total of six data types
- String String
- Number value
- Boolean Boolean value
- Null null
- Undefined Undefined
- Object Object
- Wherein String Number Boolean Null Undefined belonging to the basic data types , belonging to the Object reference data types
1 String String
1.1 String Basics
- 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;
var str = 'hello'; // 声明一个变量str并给其赋值hello
console.log(str) // 控制台输出str的内容
// 修改变量str的值为'我说:"今天天气真不错!"'
str = '我说:"今天天气真不错!"'; // 会修改掉原来str的值
console.log(str)
1.2 String Escape
In the string we can use \ as an escape character, when expressed some special symbols can be used \ to escape
- \ "=> Means"
- \ '=> Represents'
- \ N => represents a line feed
- \ T => tab
- \ => Representation \
str = "我说:\"今天\t天气真不错!\"";
str = "\\\\\\";
//输出字面量 字符串str
alert("str");
1.3 String stitching
There are two forms of string concatenation: Use the + stitching; use template string
var a = "孙悟空";
var b = "我是"+a;
console.log(b);
var c = `我是${a}`;
console.log(c);
2 Number Numeric
- In JS all values are Number types , including integer and floating point (decimal)
- JS the maximum number that can be expressed in
- Number.MAX_VALUE
- 1.7976931348623157e + 308
- Is greater than a minimum value of 0 Number.MIN_VALUE
- 5e -324
- If digital Number representation exceeds the maximum, it will return a
- Infinity represents positive infinity
- -Infinity representing negative infinity
- Use typeof check Infinity will return number
- NaN is a special number, indicates Not A Number, using typeof NaN also returns a check number
var a = 123; //数字123
var b = "123"; //字符串123
/*
可以使用一个运算符 typeof来检查一个变量的类型
语法:typeof 变量
检查字符串时,会返回string
检查数值时,会返回number
*/
// console.log(typeof b);
a = -Number.MAX_VALUE * Number.MAX_VALUE;
a = "abc" * "bcd";
a = NaN;
// console.log(typeof a);
a = Number.MIN_VALUE;
//console.log(a);
// 在JS中整数的运算基本可以保证精确
var c = 1865789 + 7654321;
/*
* 如果使用JS进行浮点运算,可能得到一个不精确的结果
* 所以千万不要使用JS进行对精确度要求比较高的运算
*/
var c = 0.1 + 0.2;
console.log(c);
3 Boolean Boolean value
- Boolean Boolean: Boolean only two, mainly used for determination logic
- true - for true
- false - false representation
- When using typeof check a Boolean value, it will return boolean
var bool = false;
console.log(typeof bool);
console.log(bool);
4 Null null and undefined Undefined
- The null value is used to represent a special null object
- When using typeof check for a null value, returns object
- Value Undefined (undefined) only one type, it undefind
- When you declare a variable, but not when assigned to a variable, its value is undefined, also returns undefined undefined when using a check typeof
var a = null;
var b = undefined;
console.log(typeof b);
Second, the data type conversion
Cast
- It refers to a data type cast to other data types
- Mainly refers to the type of conversion, the other types of data, converted to a String, Number, Boolean
1 other data type to String
- Convert other data types to String
- method one:
- Calls are converted to the data type toString () method
- This method does not affect the original variables, the results of which will be converted Return
- But note: null and undefined these two values do not toString () method, if you call their approach will complain
- Second way:
- Call String () function, and the converted data is passed to the function as a parameter
- Use String () function to do when cast for Number and Boolean actually call toString () method
- But for null and undefined , it will not call the toString () method, it will directly convert null "null", will directly convert undefined "undefined".
//调用toString()方法,来将a转换为字符串
var a = 123; // 数值转String
a = a.toString();
a = true; // 布尔转String
a = a.toString();
a = null; // null转String
a = a.toString(); //报错
a = undefined; // undefined转String
a = a.toString(); //报错
//调用String()函数,来将a转换为字符串
a = 123;
a = String(a);
a = null;
a = String(a);
a = undefined;
a = String(a);
console.log(typeof a);
console.log(a);
2 is converted to a different data type Number
2.1 Data Conversion
Other data types converted to Number
A conversion mode:
Use Number () function
String -> Digital
- 1. If the string is purely digital, it is directly converted into digital
- 2. If the content of non-numeric string, the conversion is NaN
- 3. If the string is a null string is a space or a full string, then converted to 0
Istanbul -> Digital
- converted into a true
- false turn into 0
null -> Digital 0
undefined -> Digital NaN
Conversion way:
- This way designed to deal with string
- the parseInt () to convert a string to an integer
- parseFloat () to convert a string into a float
//调用Number()函数来将a转换为Number类型
var a = "123";
a = Number(a); // 123
a = false;
a = Number(a); // 0
a = null;
a = Number(a); // 0
a = undefined;
a = Number(a); // Nan
//调用parseInt()函数将a转换为Number
a = "123567a567px";
// parseInt()可以将一个字符串中的有效的整数内容去出来,然后转换为Number
a = parseInt(a);
// parseFloat()作用和parseInt()类似,不同的是它可以获得有效的小数
a = "123.456.789px";
a = parseFloat(a);
// 如果对非String使用parseInt()或parseFloat(),它会先将其转换为String然后在操作
a = true;
a = parseInt(a);
a = 198.23;
a = parseInt(a);
console.log(typeof a);
console.log(a);
2.2 hex value
- In js, if you need to represent hexadecimal numbers, you need to begin with 0x
- If you need to represent octal numbers, you need to start with 0
- If you want to represent binary numbers, you need to begin with 0b
- But not all browsers support
//十六进制
a = 0x10;
a = 0xff;
a = 0xCafe;
//八进制数字
a = 070; //像"070"这种字符串,有些浏览器会当成8进制解析,有些会当成10进制解析
//二进制数字
a = 0b10;
a = "070";
//可以在parseInt()中传递一个第二个参数,来指定数字的进制
a = parseInt(a,10);
console.log(typeof a);
console.log(a);
3 is converted to a different data type Boolean
- Converting the other data type Boolean
- Using Boolean () function
- Digital -> Boolean
- In addition to 0 and NaN, the rest are true
- String -> Boolean
- In addition to the empty string, the rest are true
- null and undefined are converted to false
- Objects can also be converted to true
// 数值转布尔值
var a = 123; //true
a = -123; //true
a = Infinity; //true
a = NaN; //false
a = 0; //false
// 调用Boolean()函数来将a转换为布尔值
a = " "; // ture
a = null; //false
a = undefined; //false
a = Boolean(a);
console.log(typeof a);
console.log(a);
// 数值转布尔值
var a = 123; //true
a = -123; //true
a = Infinity; //true
a = NaN; //false
a = 0; //false
// 调用Boolean()函数来将a转换为布尔值
a = " "; // ture
a = null; //false
a = undefined; //false
a = Boolean(a);
console.log(typeof a);
console.log(a);