Home > Backend Development > PHP Tutorial > Some uncommon and practical functions in javascript

Some uncommon and practical functions in javascript

巴扎黑
Release: 2023-03-03 13:02:01
Original
1043 people have browsed it

It’s been 996 for almost two months. I haven’t written anything for more than a month. Today I will record some usages in JavaScript that are convenient but not commonly used

1. ~indexOf()

Generally speaking , to determine whether a string contains a substring, you can use indexOf(subString) > -1 to represent

We can also use ~indexOf(subString) to represent

indexOf() returns -1 to mean no, returns 0 And the number greater than 0 means that the result of ~-1 is 0, which is equivalent to false

in if. 0 and greater than 0 are numbers that are not 0 after the ~ operation, which is equivalent to true

2 in if. ~~

                                                                                                                                                                                                                                                                    ~ ;>0 n|0

Of course, there is a limitation in using these algorithms for rounding, that is, the range of numbers can only be used within the range of 32-bit binary numbers, that is, the numbers must be between -2147483648 ~ 2147483647. This is also a number of plus or minus 2 billion. Under normal circumstances, there is no problem.

3. This is a problem and makes me a little confused.

The method of toFixed is to round the Number to a number with specified decimal places.

The Math.round method can round the number to the nearest integer

But for -4.5.toFixed(0) and Math.round(-4.5), one returns -5 and the other returns -4.

Why is this, it makes me very confused, can anyone explain to me

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template