In the JavaScript world, everything is an object.
To summarize, there are a few rules to follow:
Do not use
new Number()
,new Boolean()
, tonew String()
create wrapper objects;Use
parseInt()
orparseFloat()
to convert any type tonumber
;Used to
String()
convert any type tostring
, or directly call atoString()
method of an object;Usually it is not necessary to convert any type to
boolean
re-judgment, because it can be written directlyif (myVar) {...}
;typeof
The operator can determinenumber
,boolean
,string
,function
andundefined
;judge
Array
to useArray.isArray(arr)
;Judge
null
please usemyVar === null
;Determine whether a global variable exists or not
typeof window.myVar === 'undefined'
;Inside the function, it is used to determine whether a variable exists
typeof myVar === 'undefined'
.
Finally, does any object have toString()
methods? null
and undefined
there is no! It does, except for these two special values, albeit null
disguised as object
types.
Also pointed out that the number
object call toString()
reported SyntaxError:
123.toString(); // SyntaxError
In this case, special treatment is required:
123..toString(); // '123', 注意是两个点!
(123).toString(); // '123'