Details about the usage and functions of the single vertical bars "|" and "|" of js Operators

Source: Internet
Author: User
Details about the usage and functions of the single vertical bars "|" and "|" of js operators. In js development applications, we usually encounter "|" and "|, so what does "|" and "|" mean in the operation?

In JavaScript integer operations, the decimal point is removed, which is parseInt. Math. floor () is equivalent to a positive number, and Math. ceil () is equivalent to a negative number. Note:

1. Math. ceil () is used as the rounded up integer.
2. Math. floor () is used for downgrading.
3. Math. round () the rounding round we often use in mathematics.

console.log(0.6|0)//0console.log(1.1|0)//1console.log(3.65555|0)//3console.log(5.99999|0)//5console.log(-7.777|0)//-7


Operation Rules for single vertical bars

After reading the example above, we generally know that the single vertical bar can be used to perform an integer operation, that is, only the positive part is retained, and the fractional part is removed, but "| 0 ", how is the computation performed? Why can "| 0" be used to achieve the goal of an integer? What if a single vertical bar is not 0?

With these questions, let's look at the following example:

console.log(3|4); //7console.log(4|4);//4console.log(8|3);//11console.log(5.3|4.1);//5console.log(9|3455);//3455


Here we mention the single vertical bar "|" but there is no javascript.

Okay, let me publish the answer here. In fact, a single vertical bar "|" is the result obtained after being converted to a binary hexadecimal notation. For example, let's take a simple example:

3 | 4

After being converted to binary, add 100 to 111 and get = 7.

4 | 4

100 after being converted to binary | 100 is added to get 100 = 4

8 | 3

1000 after being converted to binary | 1011 after the 011 value is added, = 11 is obtained.

And so on, I will not list them here. The single vertical bar "|" operation is the result obtained after being converted to a binary system!

JS double vertical line operator

1. JS double vertical line operator: yes or comparison. such as null | '1', return '1'; '2' | '1', return '2 '. that is, in the OR operator, if the first one is true, the subsequent values do not need to be calculated. so it takes '2 '.

2. Use the double vertical line operator "|" in js to return the first valid value.

var objOne = undefined || 1 || null || new Date();var objTwo = new Date();var objThree = objOne || objTwo;alert(objThree.toString()); //out put "1"


Summary

Performance Comparison

In the logical operator & |, if the first operation Number of & is false, the second operation number is not taken into account and false is directly returned; if the first operation Number of | is true, and the second operation number is not considered, true is returned directly. And the & and | operators are not like this. They always have to compare two arithmetic numbers to get the result, so the performance is better than & and |.

Function usage

& | Only logical operations can be performed, and & | in addition to "logical operations", bit operations can also be performed.

Bitwise operation

& | This is a bitwise operator. The reason why "logical operations" can be performed is that JS is a non-type language and various data types can be freely converted, when "logical operation" is performed with & |, true is actually converted to 1, false is converted to 0, and then bitwise operation is performed:

Document. write (true & false); // JS, the result is 0


In the above sentence, the instance is equivalent to the logic operation being converted to the following bitwise operation and executed:

Document. write (1 & 0); // JS, the result is 0


It is also because the & | Operator is a bit-by-bit operator that the result is obtained by comparing the numbers of two operations in the first vertex, this results in lower performance than & |.

The above is a detailed description of the usage and functions of the single vertical bars "|" and "|" of js operators. For more information, see the PHP Chinese website (www.php1.cn )!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.