JavaScript Advanced Programming (4th Edition) Reading Sharing Note Recording
Applicable to comrades who are just getting started
ECMAScript has 6 simple data types (also known as primitive types ): Undefined , Null , Boolean , Number , String and Symbol (new in es6) .
There is also a complex data type called Object (object)
Table of contents
typeof operator
Using the
typeof
operator on a value returns one of the following strings:
- "undefined" indicates that the value is undefined;
- "boolean" indicates that the value is a Boolean value;
- "string" indicates that the value is a string;
- "number" indicates that the value is a value;
- "object" means the value is an object (not a function) or null ;
- "function" indicates that the value is a function;
- "symbol" indicates that the value is a symbol.
let message = "some string";
console.log(typeof message); // "string"
console.log(typeof(message)); // "string"
console.log(typeof 95); // "number"
instanceof operator
typeof, while useful for primitive values, is less useful for reference values. To solve this problem,
ECMAScript
provides
the instanceof
operator
grammar:
result = variable instanceof constructor
The instanceof operator returns true if the variable is
an instance of
the given reference type
Table of contents
Undefined type
The Undefined
type has only one value, the special value
undefined
. When
a variable is declared using
var
or
let but has no initial
When converted, it is equivalent to assigning an
undefined
value to the variable
Null type
The Null
type also has only one value, the special value
null
. Logically speaking,
the null
value represents a null object pointer, which is also given to
The reason why typeof returns "object" when
passing a
null
let car = null;
console.log(typeof car); // "object"
Boolean type
The Boolean
(Boolean) type is
one of the most frequently used types in
ECMAScript and has two literal values:
true and false .
Conversion rules between different types and Boolean values
:
//理解以上转换非常重要,因为像 if 等流控制语句会自动执行其他类型值到布尔值的转换,例如:
let message = "Hello world!";
if (message) {
console.log("Value is true");
}
NumberType _
floating point value
let floatNum1 = 1.1;
let floatNum2 = 0.1;
NaN
There is a special value called
NaN
, which means "not a number" (
Not a Number
), and is used to indicate that the operation that was supposed to return a number failed (instead of throwing an error).
isNaN()
function: This function receives a parameter, which can be of any data type, and then judges whether the parameter is "not a value".
console.log(isNaN(NaN)); // true
console.log(isNaN(10)); // false,10 是数值
console.log(isNaN("10")); // false,可以转换为数值 10
console.log(isNaN("blue")); // true,不可以转换为数值
console.log(isNaN(true)); // false,可以转换为数值 1
Numeric conversion
There are 3
functions that convert non-numeric values to numeric values:
Number() , parseInt() , and parseFloat() .
The Number()
function performs the conversion based on the following rules:
- Boolean value, true is converted to 1 and false to 0 .
- Value, returned directly.
- null , returns 0 .
- undefined , returns NaN .
- There are corresponding rules for character strings (see page 36 of the book for details).
let num1 = Number("Hello world!"); // NaN
let num2 = Number(""); // 0
let num3 = Number("000011"); // 11
let num4 = Number(true); // 1
Usually the parseInt() function can be used first when an integer is needed
.
let num1 = parseInt("1234blue"); // 1234
let num2 = parseInt(""); // NaN
let num3 = parseInt("0xA"); // 10,解释为十六进制整数
let num4 = parseInt(22.5); // 22
let num5 = parseInt("70"); // 70,解释为十进制值
let num6 = parseInt("0xf"); // 15,解释为十六进制整数
pressFloat()
let num3 = parseFloat("22.5"); // 22.5
let num4 = parseFloat("22.34.5"); // 22.34
String type
String()
let value1 = 10;
let value2 = true;
let value3 = null;
let value4;
console.log(String(value1)); // "10"
console.log(String(value2)); // "true"
console.log(String(value3)); // "null"
console.log(String(value4)); // "undefined"
template literal
ECMAScript 6
added the ability to define strings using template literals.
Note: Since template literals will keep spaces inside backticks, extra care should be taken when using them.
let myTemplateLiteral = `first line
second line`;
console.log(myTemplateLiteral.length); // 47
string interpolation
One of the most commonly used features of template literals is support for string interpolation, which means that one or more
value.
String interpolation is achieved
using a JavaScript expression inside
${} :
let age = 25
let name = '小美';
// 以前,字符串插值是这样实现的:
let interpolatedString =
name+ ' 今年 ' + age + ' 岁了 ';
// 现在,可以用模板字面量这样实现:
let interpolatedTemplateLiteral =
`${ name } 今年 ${ age } 岁了`;
console.log(interpolatedString); // 小美今年25岁了
console.log(interpolatedTemplateLiteral); // 小美今年25岁了
Symbol type
Symbol
(symbol) is
a new data type in
ECMAScript 6 .
Symbols are primitive values, and symbol instances are unique and immutable. The purpose of the symbol is to ensure that object properties use unique identifiers without the danger of property conflicts.
The symbol type is generated by the Symbol function and is used to represent a unique value
let sym = Symbol();
console.log(typeof sym); // symbol
Values of type Symbol are different even though the same string is used to create values of type Symbol
let fooSymbol = Symbol('foo');
let otherFooSymbol = Symbol('foo');
console.log(genericSymbol == otherGenericSymbol); // false
Most importantly,
the Symbol()
function cannot
be used as a constructor with
the new
keyword
let mySymbol = new Symbol(); // TypeError: Symbol is not a constructor
Object type
An object in ECMAScript
is actually a collection of data and functions. Objects are passed through
the new
operator followed by the name of the object type
to create.
let o = new Object();