Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement type casting by js

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly describes how js type coercion, the text is very detailed, has a certain reference value, interested friends must read!

type cast 1 string cast to number

You can use *1 to convert to a number (actually calling the.valueOf method) and then use Number.isNaN to determine if it is NaN, or a !== a to determine if it is NaN, because NaN !== NaN

'32' * 1 // 32'ds' * 1 // NaNnull * 1 // 0undefined * 1 // NaN1 * { valueOf: ()=>'3' } // 3

Common: + can also be used to convert strings to numbers

+ '123' // 123+ 'ds' // NaN+ '' // 0+ null // 0+ undefined // NaN+ { valueOf: ()=>'3'}// 32 object forced to string

You can convert an object to a string by using String +Object (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 an object to customize the type conversion of the object:

2 * { valueOf: ()=>'3' } // 6'J' + { toString: ()=>'S' } // "JS"

Effective JavaScript P11: When + is used in connection strings, when an object has both toString and valueOf methods, JS resolves this ambiguity by blindly using valueOf methods.

Objects are cast to numbers by the valueOf method and to strings by the toString method

'' + {toString:()=>'S', valueOf:()=>' J'}// J3 Filter all false values in an array using Boolean

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 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 ]4 binary operator ~~

Two-bit operators can be used instead of Math.floor( ). The advantage of the double negation operator is that it performs the same operation faster.

Math.floor(4.9) === 4 //true//shorthand: ~~4.9 === 4 //true

Note, however, that the ~~ operation results in the same result as Math.floor( ) for integers, but not for negative numbers:

~~4.5 // 4Math.floor(4.5) // 4~~-4.5 // -4Math.floor(-4.5) // -55 Short-circuit operator

We know logic and && and logic or|| The short-circuit operator is an operation from left to right in which the former meets the requirements and the latter is no longer executed; it can be understood as:

&& is a false operation, judging from left to right, if a false value is encountered, it returns a false value, and will not be executed later, otherwise it returns the last true value.

||For true operation, judge from left to right in turn. If a true value is encountered, it will return the true value. It will not be executed later. Otherwise, it will return the last false value.

let param1 = expr1 && expr2let param2 = expr1 ||expr2 operator example description &&expr1&&expr2 returns expr1 if expr1 can be converted to false, otherwise returns expr2. Therefore, when used in a Boolean environment, returns true if both operations are true and false otherwise.|| expr1|| expr2 Returns expr1 if expr1 can be converted to true, otherwise returns expr2. Therefore, when used in boolean environment (in the conditional judgment of if), if one of the two operation results is true, return true; if both operation results are false, return false.!! expr Returns false if a single expression can be converted to true, otherwise true.

So it can be used to do a lot of interesting things, such as giving variables initial values:

let variable1let variable2 = variable1 || 'foo'

If variable1 is true, it will be returned directly, and the short circuit will not be returned. If it is false, it will return foo.

It can also be used to make simple judgments instead of lengthy if statements:

let variable = param && param.prop

If param is true, it returns the param.prop attribute, otherwise it returns the param false value, which prevents param from taking its attribute when undefined in some places and causing an error.

6 rounded| 0

Yeah, a number.| 0 can be rounded off, negative numbers also apply, num| 0

1.3 | 0 // 1-1.9 |0 // -17 Judge parity & 1

For a number & 1 can determine even and odd numbers, negative numbers also apply, num & 1

const num=3;!! (num & 1) // true!! (num % 2) // true The above is "js how to implement type casting" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report