Home >Web Front-end >JS Tutorial >Using the special features of the operators '||' and '&&' in JScript to achieve code streamlining_javascript skills

Using the special features of the operators '||' and '&&' in JScript to achieve code streamlining_javascript skills

PHP中文网
PHP中文网Original
2016-05-16 19:17:441023browse

Among the many operators in JScript, it provides three logical operators &&, || and !, oh?! It is a high-level language. provided. According to our normal understanding of logical operations, the result of a logical operation should be true or false. However, JScript’s logical operations are not entirely defined in this way. There are only ! operators that always return true|false, and the || and && operations are more fun.

JScript’s definition of true|false for logical operations is as follows:

  • All objects are considered true .

  • A string is considered false if and only if it is empty ("" or '').

  • null and undefined are considered false.

  • Number is false if and only if it is 0.

But although the logical operators || and && follow the above definition rules, the values ​​they return are very interesting.
For the && operation, according to the above rules, the expression if ('abc' && '123' && new Date() ) executes the true branch, but if this expression is written as:

Using the special features of the operators '||' and '&&' in JScript to achieve code streamlining_javascript skills var value = 'abc' && '123' && new Date();

Result value=Fri Jan 21 00:01:17 UTC 0800 2005. It originally checked from left to right. If the last expression is also true, that expression is returned.

The same goes for the || operation, for the following expression:

Using the special features of the operators '||' and '&&' in JScript to achieve code streamlining_javascript skillsvar value1 = 'abc' || '123' || null || false;
Using the special features of the operators '||' and '&&' in JScript to achieve code streamlining_javascript skills
var value2 = null || ' '||false 🎜>

Result value1='abc', value2='ok'. This is because the || operation has a "short circuit" characteristic. It is also tested from left to right, but it returns the expression immediately as soon as it finds a true value.
Such a feature can help us write streamlined code, but it also brings about the problem that the code is not easy to read and maintain.
Since I don’t have browsers like NS and moz at hand, I wonder if standard JavaScript is also supported in this way? If it's convenient for you, please let me know the results after running it.

In JScript, these logical operations are all used to determine whether an expression is "meaningful"

For example, 0, "", null, false, undefined, NaN... etc. are none Meaningful..

&& will return true or false
while a||b is, if a is meaningful, return a, otherwise return b

So sometimes , I don’t write code like this:
var obj=QuerySomeObject();
if(obj==null)return null;
return obj.Property;

I will write it as
return (QuerySomeObj()||{}).Property;
In addition, please pay attention to the comparison between null and false.
0 "" and false are all equal when compared with ==.

But null is different. For example

if(!null)
{
alert(1);
}
if(null==false)
{
alert(2);
}
Tested in Firefox, the result is the same!

Statement:
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