Explanation on the use of js operators single vertical bar '|' and '||'
In js development applications, we usually encounter "|" and "||", so what do "|" and "||" mean in operations? This article mainly introduces the detailed explanation of the usage of js operators single vertical bar "|" and "||". You can learn more if you need it.
In js development applications, we usually encounter "|" and "||", so what do "|" and "||" mean in operations?
When performing js integer operations, it is equivalent to removing the decimal point, parseInt. When it is a positive number, it is equivalent to Math.floor(), when it is a negative number, it is equivalent to Math.ceil() Note:
1. Math.ceil() is used to round up.
2. Math.floor() is used to round down.
3. Math.round() Rounding is commonly used in our mathematics.
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
Operation rules of single vertical bars
After reading the above example, we generally know that single vertical bars can be used for rounding operations , that is, only the positive part is retained, and the decimal part is removed. But how is "|0" calculated? Why can "|0" achieve the purpose of rounding? If the horizontal and vertical bar is not 0, what would it be?
With these questions, let’s look at the following example:
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
The horizontal and vertical bar "|" is mentioned here but there is no javascript.
Okay, let me announce the answer here. In fact, the single vertical bar "|" is the result of the addition after converting to binary. For example, let’s take a simple example:
3|4
After converting to binary, 011|100 is added to get 111=7
4|4
After converting to binary, 100 | 100 is added to get 100=4
8|3
After being converted to binary, 1000 | 011 is added to get 1011=11
By analogy, I will not list them one by one here. The single vertical bar "|" operation is the result of the addition after converting to binary!
JS double vertical bar operator
1. JS double vertical bar operator: yes or comparison. Such as null||'1', return '1'; '2'||'1', returns '2'. That is, in the OR operator, the first one is true, and the following ones do not need to be calculated. So we get '2'.
2. Use the double vertical bar 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 operand of && is false, the second operand will not be considered operand, return false directly; if the first operand of || is true, the second operand will not be considered, and true will be returned directly. This is not the case with the & and | operators. They always need to compare two operands to get the result, so the performance of && and || will be better than & and |.
Function Usage
&& and || can only perform logical operations, while & and | can not only perform "logical operations", but also perform bit operations
Bit operations
&| are originally bit operators. The reason why "logical operations" can be performed is because JS is a typeless language and each data type can be freely Determined by this feature of conversion, when using & and | to perform "logical operations", true is actually converted to 1, false is converted to 0, and then bit-by-bit operations are performed:
document.write(true & false); //JS,结果为0
The above sentence is equivalent to logical operations being converted into the following bit operations and executed:
##
document.write(1 & 0); //JS,结果为0It is precisely because & and | are sequential Bitwise operators appear as mentioned in the first point. They always need to compare two operands to get the result, which results in lower performance than && and ||.
What are the ways to encrypt passwords in nodejs
vue processes the data obtained by storejs
nodejsDetailed explanation of the steps to connect to the mysql database
The above is the detailed content of Explanation on the use of js operators single vertical bar '|' and '||'. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
