Detailed JS operator single vertical bar "|" and "| |" Introduction to the use and role of the basic knowledge

Source: Internet
Author: User
Tags bitwise bitwise operators logical operators

In the JS development application we usually encounter the "|" and "| |" , then in the operation "|" and "| |" What does it mean?

In the JS integer operation, the equivalent of removing the decimal point, parseint. When a positive number is equivalent to Math.floor (), a negative number is equivalent to the Math.ceil () Note:

1. Math.ceil () is used for rounding up.
2. Math.floor () is used as a downward rounding.
3. Math.Round () The rounded rounding we used in maths.

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

Rule of operation of single vertical bar

Look at the above example, the general know that a single vertical bar can be used to take the whole operation, that is, only the positive part, the decimal part by taking off, but "|", and how to operate it, why can "|" to achieve the purpose of rounding? The single vertical bar is not 0 What is the number?

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

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

This refers to the single vertical bar "|" But there's no JavaScript.

All right, I'm here to announce the answer. In fact, single vertical bar "|" Is the result of the addition of the conversion to the 2 binary. For example, let's take a simple example:

3|4

After converting to binary 011|100 add to 111=7

4|4

After converting to binary 100 |100 add to get 100=4

8|3

After converting to binary 1000 |011 add to get 1011=11

And so on, I'm not going to enumerate here, single vertical bar "|" The operation is to convert to 2 after the addition of the result!

JS Double vertical bar operator

1, JS double vertical line operator: is or comparison. such as null| | ' 1 ', return ' 1 '; 2 ' | | ' 1 ', return ' 2 '. The first is true, and the back is not calculated. So ' 2 '.

2, JS using the double vertical bar operator "| |", 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"

Summarize

Performance comparisons

Logical Operators && | | , if the && 's first shipping count is false, the second operand is no longer considered and returns false directly; The first op count is true, and the second operand is no longer considered, and returns true directly. The & and | operators are not, and they always have to compare two operands to get results, so performance && and | | would be better than & and |

Functional usage

&& and | | Only logical operations can be performed, and & and | In addition to being able to perform "logical operations", bitwise operations

Bit operations

& and | This is a bit operator, the reason for "logical operation" is because JS is a language of no type, the data types can be freely converted by the characteristics of the decision, when using & and | "Logical operation", actually true is converted to 1,false is converted to 0, The bitwise-BY Operation:

Document.Write (True & false); JS, the result is 0

In the preceding sentence, an instance is equivalent to a logical operation being converted to the following bitwise operation and executing:

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

It is also because & and | are bitwise operators that appear in the 1th, they always have to compare two operands to produce results, which will result in performance than && and | | A little lower.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.