A summary of judgment in JavaScript code _javascript tips

Source: Internet
Author: User
Tags true true

Comparative judgment

Comparison judgment is to compare two values and return a Boolean value that indicates whether the comparison condition is met. JavaScript provides a total of 8 comparison operators, and here's the difference between the strict equality and equality operators

Strict equality operator = = =

    Judgement            returns
two value types different            false
two values are null/undefined/true/false true   
two values one of them is Nan         false
Two values are numeric and the value is equal to         true
two values are strings, and the value is equal to       true
two values all point to the same reference type       true

1 = = "1"//false
true = = = TRUE//true
undefined = = undefined/true
NULL = = NULL//true
1 = = 1/true
nan = = nan//F Alse
+0 = = 0/True
({} = = = {})//
false [] = = = []//False
(function () {} = = function () {})//FA LSE
var v1 = {};
var v2 = v1; Two values refer to the same object
v1 = = V2/True

The strict equality operator has a corresponding strict inequality operator (!==), which results in the exact opposite

Equality operator = =

If the If equality operator compares data of the same type when
comparing different types of data with the strict equality operator else if equality operator:
The original type of data is converted to a numeric type, and the string and Boolean values are translated into numeric values, and then
null = = Undefined returns True
one is an object, the other is a number or a string, and the object is converted to a basic type value, which is more than
else false

123 = = 123;//true
' 123 ' = 123;//true, ' 123 ' will be converted to a value
of 123 false = = 0;//true,false converted to a value is 0
' a ' = = ' a ';//false, the converted encoding is not the same
123 = = {};//false, execute ToString ( or valueof () will change
123 = = Nan;//false, as long as there is Nan, is false
{} = = {};//false, compares their addresses, each newly created object's reference address is different

null = = Undefined//true
' nan ' = = Nan//false
123 = = Nan//false
nan = = Nan//false
false = 0//true
tru E = 1//true
true = = 2//false
undefined = 0//false
null = 0//false
' 123 ' = 123
//true ' 123 ' = = = 123//false

The equality operator has a corresponding inequality operator (!=), which results in the exact opposite

!! Judge

!! Equivalent Boolean , write code with!! Conversion to Boolean type makes judgment very handy

!!' Xzavier ';  True
!! ';     False
!! ' 0 ';     True
!! ' 1 ';     True
!! ' -1 '     //True
!! 0      //False
!! Undefined  //False
!! Null     //False
!! NaN     //False
!! {};     True
!! {name: ' XZ '}//True
!! [];     True
!! [1,2,3];   True
!! true;    True

! judge

Take the counter operator! Used to change the Boolean value to the opposite value, that is, true becomes false,false. For data that is not a Boolean value, the counter operator automatically converts it to a Boolean value. The rule is that the following six values are reversed to true, and the other values are reversed to false

Undefined null false 0 (includes +0 and -0) NaN empty string (')

!undefined  //True
!null     //True
!false    // True
!0      //True
! NaN     //True
! ""      True  
!54      //False
! ' Hello '   //False
![]      False
![ 1,2,3]   //False
!{}      False
!{ Name: ' XZ '}//False

[] and {} to determine

for [] and {}, we will certainly judge it in the business code, as

!! {};     True
!! {name: ' XZ '}//True
!! [];     True
!! [1,2,3];   True

No use!! and! To make judgments, for arrays, we use its length properties to make judgments

[].length    //0 false
[1,2,3].length//3 True

object, you can use the method of jquery $.isEmptyObject(obj) , or JS Package a method, imitate jquery to write a

function Isemptyobject (obj) {
  var name;
  for [name in obj] {return
    false;
  }
  return true;
}
Isemptyobject ({}); True

Recommend a tool library underscore , it also has a methodisEmpty(object)

Const _ = require (' underscore ');
_.isempty ({}); True

&& judgment

Used in conditional expressions, the rules are:

NUM1 && num2  true
to True false true true false False  false  

Used in statements, the rules are:

result = expression1 && expression2

If the expression1 evaluates to False, result is expression1. Otherwise, result is expression2.

(1-1) && (x + + 1)//0
(2 > 1) && (5 + 5)  //Ten
(2 + 1) && (5 + 5)  // 10

|| Judge

Used in conditional expressions, the rules are:

NUM1 | | Num2 true True false true false  false  False

Used in statements, the rules are:

If the first operator has a Boolean value of True, the value of the first operator is returned, and the second operator is no longer evaluated

Returns the value of the second operator if the Boolean value of the first operator is false

|| Operators generally in the business code to do conditional expression judgment and fault-tolerant processing, we can not get the data in the case, without affecting the following business code, it needs to be fault-tolerant. is a very good fault-tolerant notation, which is equivalent to providing an alternate data.

var data = undefined | | Backup_data; Request error, when the data is undefined, go to standby data backup_data

Three eyes judgment

Rules:

Condition? Expression1:expression2;

function ABSN (xzavier) {return
  xzavier > 0? xzavier:-xzavier;
}
ABSN (-123); 123
ABSN (123);//123

Returns the value of the second expression if the Boolean value of the first expression is true, otherwise the value of the third expression is returned.

Summarize

That's all you need to do to sort out the entire contents of the JavaScript code, and hopefully this article will help you with JavaScript.

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.