Home >Web Front-end >JS Tutorial >Tips in JS that can improve happiness
This article mainly introduces some tips used in JS, which can improve happiness in daily coding. It will be updated from time to time~
Interested students can join Let’s discuss together in the WeChat group at the end of the article~
can be converted using *1
is a number (actually calling the .valueOf
method)
Then use Number.isNaN
to determine whether it is NaN
, or use a !== a
to determine whether it is NaN
, because NaN !== NaN
'32' * 1 // 32'ds' * 1 // NaNnull * 1 // 0undefined * 1 // NaN1 * { valueOf: ()=>'3' } // 3复制代码
Commonly used: You can also use
to convert a string into a number
+ '123' // 123+ 'ds' // NaN+ '' // 0+ null // 0+ undefined // NaN+ { valueOf: ()=>'3' } // 3复制代码
, you can use the String Object
method to convert the object into a string (actually calling the .toString()
method)
'the Math object:' + Math // "the Math object:[object Math]"'the JSON object:' + JSON // "the JSON object:[object JSON]"复制代码
Of course, you can also override the toString
and valueOf
methods of the object to customize the type conversion of the object:
2 * { valueOf: ()=>'3' } // 6'J' + { toString: ()=>'S' } // "JS"复制代码
《Effective JavaScript》P11: When
is used when connecting strings. When an object has both a
toString
method and avalueOf
method, JS solves this problem by blindly using thevalueOf
method. Kind of vague. The object is forced to be converted to a number through thevalueOf
method, and to a string through thetoString
method
'' + {toString:()=>'S',valueOf:()=>'J'} // J复制代码
We know that there are some false values in JS: false
, null
, 0
, ""
, undefined
, NaN
, how to quickly filter false values in the array, you can use the Boolean constructor to perform a conversion
const compact = arr => arr.filter(Boolean) compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]) // [ 1, 2, 3, 'a', 's', 34 ]复制代码
You can use double-bit operators to replace Math.floor( )
for positive numbers and Math.ceil( )
for negative numbers. The advantage of the double-negative positioning operator is that it performs the same operation faster.
Math.floor(4.9) === 4 //true// 简写为:~~4.9 === 4 //true复制代码
But please note that for positive numbers, the result of the ~~
operation is the same as that of Math.floor( )
, and for negative numbers, it is the same as Math.ceil( )
has the same operation result:
~~4.5 // 4Math.floor(4.5) // 4Math.ceil(4.5) // 5~~-4.5 // -4Math.floor(-4.5) // -5Math.ceil(-4.5) // -4复制代码
We know that logical AND&&
and logical OR||
is a short-circuit operator. The short-circuit operator means that if the former meets the requirements in the operation from left to right, the latter will no longer be executed;
It can be understood as:
&&
is a false operation, which is judged from left to right. If a false value is encountered, a false value will be returned and will not be executed again. Otherwise Return the last true value ||
is a true operation, judged from left to right, if a true value is encountered, the true value will be returned and will not be executed again, otherwise it will be returned Last false valuelet param1 = expr1 && expr2let param2 = expr1 || expr2复制代码
Operator | Example | Description |
---|---|---|
& |
expr1&&expr2 |
If expr1 can be converted to false, expr1 is returned, otherwise expr2 is returned. Therefore, in When used in a Boolean environment, true is returned when both operation results are true, otherwise false is returned. |
|| | expr1||expr2 | If expr1 can be converted to true, return expr1, otherwise return expr2. Therefore, when used in a boolean environment (in the conditional judgment of if), as long as one of the two operation results is true, true will be returned; the results of both operations Returns false when both are false. |
##!
| ##!expr
If a single expression If it can be converted to true, it returns false, otherwise it returns true. |
The above is the detailed content of Tips in JS that can improve happiness. For more information, please follow other related articles on the PHP Chinese website!