Type:
There are 7 built-in types of JavaScript
Null value (NULL)
Not defined (undefined)
Boolean value (Boolean)
Digital (number)
string (string)
Object (object)
symbols (symbol)
other than objects, collectively referred to as " basic types "
To view the type of a value using the TypeOf operator
typeof undefined = = = "undefined"; True
typeof true = = = "Boolean"; True
typeof = = = "Number"; True
typeof "a" = = = = "string"; True
typeof {Life:42} = = = "Object"; True
typeof symbol () = = = "Symbol"; True
typeof null = = = "Object"//True
we need to use compound conditions to detect the type of a null value
var a = null;
(!a && typeof a = = = "Object"); True
Null is a false value is the only basic type that will return "Object" with typeof detection
A function is a subtype of an object, a function is a callable object, and he has an internal property [Call] This property enables it to be called.
The length property of a function object is the number of arguments it declares:
function A (a,c) {
}
A.length; 2
An array is also a subtype of an object whose length is a number of elements.
typeof [N/a] = = = = "Object"; True
Values and Types
the variables in the javascrpit are of no type, only the values, and the variables can hold any type of value at any time.
The typeof operator always returns a string:
typeof typeof 42; "String"
typeof First returns the string "number" and then typeof " number" back to "string"
Undefined and undeclared
A variable is undefined when it has been declared but not assigned a value .
variables that are not declared in the scope are undeclared .
but often these two situations will be undefined, the browser handles the problem.
Global variables can be passed through window. to access.
Array:
Creating sparse arrays with both blank or empty arrays
var a = [];
A[0] = 1;
a[1] unit not set here
A[2] = [3];
A[1]; Undefined
A.length; 3
it can be indexed by numbers, or it can contain string key values and attributes. ( recommended numeric index )
var a = [];
A[0] = 1;
a["Foobar"] = 2;
A.length; 1
a["Foobar"]; 2
A.foobar; 2
Class Array
tool function slice (...) is often used for type conversions
function foo () {
var arr = Array.prototype.slice.call (arguments);
Arr.push ("Bam");
Console.log (arr);
}
Foo ("Bar", "Baz"); ["Bar", "Baz", "Bam"]
ES6 built-in Tools array.from (...) can also achieve the same function.
var arr = Array.from (argments);
String
To handle a string by borrowing the non-changing method of an array
var a = "Foo";
A.join; Undefined
A.map; Undefined
var C = Array.prototype.join.call (A, "-");
var d = Array.prototype.map.call (A,function (v) {
return v.touppercase () + ".";
}). Join ("");
C "F-o-o"
D "F.O.O"
The string is converted to an array before the result is processed back to the string.
var a = "Foo";
var C = A
Converts the value of a to a character array
. Split ("")
reverses the characters in the array
. Reverse ()
stitching the characters in an array back into a string
. Join ("");
C "Oof"
Digital
JavaScript has only one numeric type: number ( numeric );
The syntax of a number
Numeric constants are generally expressed in decimal notation
var a = 42;
var b = 42.3;
the 0 preceding the number can be ignored:
var a = 0.42;
var b =. 42;
the last 0 of the decimal part after the decimal point can be ignored.
var a = 42.0;
var B =;(. It is not recommended to write )
a particularly large or very small number is shown by default in exponential format , with the same result as the toexponential () function output.
var a = 5E10;
A
A.toexponential (); "5e+10"
var B = A * A;
b 2.5e+21
var c = 1/a;
C 2e-11
specify the number of digits to display in decimals
var a = 42.59;
a.tofixed (0); "43"
A.tofixed (1); "43.6"
A.tofixed (2); "43.59"
A.tofixed (3); "43.590"
Toprecision (..) method is used to specify the number of displays for a valid digit
var a = 42.59;
A.toprecision (1); "4e+1"
A.toprecision (2); "43"
A.toprecision (3); "42.6"
A.toprecision (4); "42.59"
A.toprecision (5); "42.590"
(for numeric variables, also for numeric constants )
A smaller number
0.1 + 0.2 = = 0.3; False
can use Number.epsilon to compare whether two numbers are equal (within the specified error range)
Security range for integers
The maximum number is 2^53-1, which is 9007199254740991 number.max_safe_integer
the minimum integer is -9007199254740991 number.min_safe_integer
Integer detection
to detect whether a value is an integer, ES6 Number.isinteger (...)
Number.isinteger (42); True
Number.isinteger (42.3); False
three-bit signed integer
Although integers can reach the maximum number of digits, some numeric operations are only used in numbers .
Special values
Value that is not a value
the undefined type has only one value, which is undefined
The null type also has only one value, which is null
Undefined
void operator
Undefined is a built-in identifier that has a value of undefined, through void
operation to get this value.
Special numbers
Numbers that are not numbers
The operand of a mathematical operation is not a numeric type, and a valid number cannot be returned, which
the value returned in the case is NaN.
A number that is not a number and is still a numeric type.
The execution of the math operation did not succeed, which is the result of a failed return.
Infinite number
Positive Infinity number Infinity
negative Infinity number -infinity
Special equations
ES6 object.is (...) to determine whether the two values are absolutely equal.
var B =-3 * 0;
Object.is (b,-0); True
Object.is (b,0); False
Values and references
There is no syntactic difference between the assignment/passing of values and references in JavaScript.
var a = 2;
var B = A; b is A copy of the value of a.
b++ ;
A 2
b 3
var c = [n/a];
var d = c; D is A reference to [all-in-one ]
D,push (4);
C [1,2,3,4]
D [1,2,3,4]
simple values are assigned, passed, unll,undefined, strings, numbers, Booleans and symbol by value copying
compound values, object functions, are assigned values/passes by reference to replication .
Because the reference points to the value itself rather than the variable, one reference cannot change the other reference to point to.
var a = [1,2,3,4];
var B = A;
A [1,2,3,4]
b [A]
then
b = [n/a];
A [A]
b [4,5,6]
You don't know. JavaScript notes (4)