Understanding JavaScript Operators

Source: Internet
Author: User

+ Operators can be used for addition operations or string connections. If you want an addition operation, make sure that the values of both expressions are numbers. Typeof many people think that typeof is a function. In fact, it is an operator and the call method is typeof hello. Of course, the function-based call method can also be: typeof (hello ), we recommend the operator call method. The typeof operator is used to determine the type of a variable. Its return values include "number", "string", "boolean", "undefined", "function", and "object ". If the number of operations is an array or null, the result is "object", which is inaccurate. Typeof is the only operator that can operate on undefined variables. It cannot operate on undefined variables at any time .!!!! It cannot be regarded as an operator, but two! It is mainly used to convert a given operand to a boolean type, so that it can be conveniently used for condition judgment, such as var a = {},!! The value of a is true, so if (!! A) The condition is true. & The operating principle is that true is returned when the expressions on both sides of the operator are true. Otherwise, false is returned. In JavaScript, if the value of the first expression is false, the value of the first expression is returned. Otherwise, the value of the second expression is returned. For example, the value of 0 & 2 is 0, not false. The value of ''& 2 is'', and the value of 2 & {} is {}, rather than false. | The principle is that if the value of the first expression is true, the value of the first expression is returned. Otherwise, the value of the second expression is returned. For example, the value of 2 | 0 is 2, and the value of [] | 2 is [], and the value of 0 | 2 is 0. This feature is most often used to assign default values to variables. For example, a = a | {}; this is a common method to create an empty object. If a already exists, assign the value to itself; otherwise, assign the null object {} to variable. Other usage methods include a = B | 3; equivalent to the if (B) {a = 3;} deletedelete operator, which deletes references to previously defined object attributes or methods. For example, var o = {a: 1}, delete o. a; attribute a is deleted. The delete operator can also be used to delete array elements, for example, a = [1, 2, 3], delete a [1]; now the value of a is [1, undefined, 2], it only converts the corresponding element into undefined, and the array length remains unchanged. Note: delete cannot delete original attributes that come with the system. The voidvoid operator returns undefined for any value. This operator is usually used to avoid output values that should not be output. For example, when the <a> element of HTML calls the JavaScript function. To do this correctly, the function cannot return valid values. Otherwise, the browser will clear the page and only display the function results. ==== Is used to assign values, ===is used to determine whether the values of expressions on both sides are equal. ===is used to determine whether the values and types of expressions on both sides are equal, because JavaScript is a weak type language, the type is unknown During Variable Declaration. We recommend that you always use ===to determine whether the expression value is equal. The newnew operator is used to create objects using constructors. For example: new Book (), or new Book, The parentheses below are not required. For details, refer to the principle of JavaScript constructor. The function name + () indicates that the function is executed. For example, var hello = function () {}, and hello (), the hello function is executed.

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.