Summary of JavaScript code and javascript code

Source: Internet
Author: User

Summary of JavaScript code and javascript code

Comparison and judgment

The comparison condition compares two values and returns a Boolean value indicating whether the comparison condition is met. JavaScript provides a total of eight comparison operators. Here we mainly talk about the differences between strict equality operators and equality operators.

Strict equality operator =

Returns two values of different types of values. The values are both null, undefined, true, and false. One of the two values is NaN and false. Both values are numerical values and the value is true. Both values are strings, and the value is equal to true. Both values point to the same reference type true1 = "1" // falsetrue = true // trueundefined = undefined/truenull = null // true1 = 1 // trueNaN = NaN/false + 0 =-0 // true ({}== {}) // false [] === [] // false (function () {}== function () {}) // falsevar v1 = {}; var v2 = v1; // two values reference the same object v1 === v2 // true

The strict equality operator has a corresponding strict inequality operator (! =), The results of the two operations are the opposite

Equal operator =

When the if equal operator compares data of the same type, the same strict equality operator else if equal operator compares data of different types: The data of the original type is converted to the numerical type, convert the string and boolean values into values, and then compare null = undefined to return true. One is an object, and the other is a number or string, convert the object to a basic type value and compare else false123 = 123; // true '123456' = 123; // true, '20180101' is converted to a value of 123 false = 0; // true, and false to A value of 0 'A' = 'a'; // false, the converted encoding is not the same as 123 =={}; // false. If toString () or valueOf () is executed, it will change 123 = NaN; // false, as long as there is NaN, both are false {}={}; // false, which compares their addresses, the reference address of each newly created object is different. null = undefined // true 'nan '= NaN // false123 = NaN // falseNaN = NaN // falsefalse = 0 // truetrue = 1 // truetrue = 2 // falseundefined = 0 // falsenull = 0 // false '000000' = 123 // true '000000' = = 123 // false

The equality operator has a corresponding inequality operator (! =), And the calculation results are the opposite.

!! Judgment

!! EquivalentBoolean, Used to write code !! ConvertBooleanIt is very useful to determine the type.

!!'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

! Judgment

Inverse Operator! Convert a Boolean value to an opposite value, that is, convert true to false, and convert false to true. For non-boolean data, the return operator automatically converts it to a Boolean value. The rule is that the values of the following six values are reversed and true, and those of other values are reversed and false.

Undefined null false 0 (including + 0 and-0) NaN null 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 {} judgment

For [] and {}, we will definitely judge the Business Code, as shown above

!!{};     // true!!{name:'xz'} // true!![];     // true!![1,2,3];   // true

Not available !! And! For the array, we use itslengthAttribute judgment

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

The jQuery method can be used for objects.$.isEmptyObject(obj) You can also encapsulate a method in js to simulate jQuery and write

function isEmptyObject(obj) {  var name;  for ( name in obj ) {    return false;  }  return true;}isEmptyObject({}); //trueisEmptyObject({name: 'xzavier'}); false 

Recommended tool Libraryunderscore, It also has a methodisEmpty(object)

const _ = require('underscore');_.isEmpty({}); // true

& Judgement

Used in conditional expressions, the rule is:

num1 && num2true  true  truetrue  false  falsefalse  true  falsefalse  false  false

Used in statements, the rule is:

result = expression1 && expression2

If the calculated result of expression1 is false, the result is expression1. Otherwise, the result is expression2.

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

| Judgment

Used in conditional expressions, the rule is:

num1 || num2true  true   truetrue  false  truefalse  true   truefalse  false  false

Used in statements, the rule is:

If the Boolean value of the first operator is true, the value of the first operator is returned, and the value of the second operator is no longer evaluated.

If the Boolean value of the first operator is false, the value of the second operator is returned.

| Operators are generally used for condition expression judgment and fault tolerance Processing in Business Code. If we cannot retrieve the data and cannot affect the subsequent business code, we need to implement fault tolerance. | Is a very good fault tolerance Statement, which is equivalent to providing a backup data.

Var data = undefined | backup_data; // request error. If the data is undefined, backup_data

Three-view judgment

Rules:

condition ? expression1 : expression2;function absN(xzavier) {  return xzavier > 0 ? xzavier : -xzavier;}absN(-123); //123absN(123); //123

If the Boolean value of the first expression is true, the value of the second expression is returned. Otherwise, the value of the third expression is returned.

Summary

The above is all the content of the judgment in JavaScript code. I hope the content in this article will be helpful for you to use Javascript.

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.