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

What is the concept of javascript logical operator

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "what is the concept of javascript logical operator?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the concept of javascript logical operator" article.

In JavaScript, logical operators are special symbols for concatenating statements into more complex statements. Logical operators can be used to represent logical operations such as "take the opposite", "or", and "and"; in JavaScript, there are "!", "& &" and "| |".

The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.

What is the meaning of javascript logical operator

Logical operators are used to represent thoughts such as "and", "or", "unless" in daily communication.

Logical operators: in formal logic, logical operators or logical conjunctions connect statements into more complex statements.

For example, suppose there are two logical propositions, "it's raining" and "I'm in the house". You can turn them into complex propositions: "it's raining, I'm in the house" or "it's not raining" or "if it rains, I'm in the house". A new statement or proposition consisting of two sentences is called a compound statement or a compound proposition.

The reason why there are two different forms of operators for "and" and "OR" is that they operate at different priorities. Operators are used to perform program code operations, which are performed on more than one Operand item.

Logical operators in JavaScript

First, the rules for converting other data types to Boolean types:

Null, undefined, 0, NaN, empty strings are converted to false, others are converted to true.

There are three logical operators in javascript:

1. Get it backwards!

First convert the data to a Boolean value, then invert it, and the result is true or false

Var a = [1Jing 2jue 3]; var b = "hello"; var obj = new Object (); var dash console.log (! "); console.log (! d); console.log (! a); console.log (! b); console.log (! obj)

two。 Logic and & &

The logic in js is different from other languages. If the first Operand is true (or can be converted to true), the result is the second Operand. If the first Operand is false, the result is false (short circuit calculation). For some special values, the above rules are not followed.

Var a = [1je 2jue 3]; var b = "hello"; var obj = new Object (); var dash console.log (true & & 10); / / the first Operand is true, resulting in the second operation, that is, 10console.log (false & & b); / / the first Operand is false, resulting in flaseconsole.log (100 & & false); / / the first Operand is 100, resulting in flaseconsole.log (undefined & false) / / the first Operand is undefined, resulting in undefinedconsole.log (NaN & & false); / / the first Operand is NaN, resulting in NaNconsole.log (null & & false); / / the first Operand is null, resulting in nullconsole.log ('& & false); / / the first Operand is an empty string, resulting in an empty string console.log (0 & 100); / / the result is 0console.log (5 & 100); / / 100console.log (a & b) / / helloconsole.log (obj & & 200)

3. Logical or | |

If the first Operand is not false, the result is the first Operand, otherwise the result is the second Operand. If the first Operand can be converted to true, the result is the first Operand

Var a = [1mem2jue 3]; var b = "hello"; var obj = new Object (); var dscape console.log (true | | 10); / / the first Operand is true, and the result is trueconsole.log (false | | b); / / the first Operand is false, resulting in the second Operand bconsole.log (100 | | false); / / the first Operand is 100, resulting in 100console.log (undefined | | 9). / / the first Operand is undefined to false, resulting in 9console.log (NaN | | false); / / the first Operand is NaN to false, resulting in the second Operand console.log (null | | a); / / the first Operand is null to false, resulting in aconsole.log ('| | false); / / the first Operand is empty string to false, resulting in second Operand console.log (0 | 100); / / the result is 100console.log (5 | 5 | 100) / / 5console.log (a | | b); / / aconsole.log (obj | | 200); / / obj above is the content of this article on "what is the concept of javascript logical operator". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow 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