Home >Web Front-end >JS Tutorial >Deep understanding of JavaScript operators

Deep understanding of JavaScript operators

高洛峰
高洛峰Original
2016-11-26 16:28:241508browse

The

+ operator can perform addition operations or string concatenation. If what you want is addition, make sure the values ​​of both expressions are numbers.

typeof

Many people think that typeof is a function. In fact, it is an operator. The calling method is typeof hello. Of course, the functional calling method can also be: typeof(hello). The operator calling method is still recommended here.

The typeof operator is used to determine the type of a variable. Its return values ​​are "number", "string", "boolean", "undefined", "function" and "object". If the operand is an array or null, the result is "object", which is inaccurate.

typeof is the only operator that can operate on undefined variables. Undefined variables cannot be operated on at any other time.

!!

!! is not one operator, but two! Continuous operation, it is mainly used to convert the given operand into a Boolean type, so that it can be conveniently used for conditional judgment, such as var a = {}, the value of !!a is true, so if (!!a ) condition is true.

&&

&& works by returning true when the expressions on both sides of the operator are true, otherwise it returns false. In fact, 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: 0 && 2 has the value 0, not false, '' && 2 has the value '', 2 && {} has the value {}, not false.

||

|| works by returning the value of the first expression if the value of the first expression is true, otherwise it returns the value of the second expression. For example: 2 || 0 has the value 2, [] || 2 has the value [], 0 || 2 has the value 0.

The most commonly used occasion for this feature is to assign default values ​​to variables. For example: a = a ||{}; This is a common way to create an empty object. If a already exists, assign it to itself, otherwise assign the empty object {} to the variable a. Other usages are a = b || 3; equivalent to if (b) { a = 3;}

delete

delete operator deletes references to previously defined object properties 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 just changes the corresponding element into undefined, the array length remains unchanged. Note: delete cannot delete the original attributes that come with the system.

void

void operator returns undefined for any value. This operator is typically used to avoid outputting a value that shouldn't be output, for example when calling a JavaScript function from an HTML element. To do this correctly, the function must not return a valid value, otherwise the browser will clear the page and display only the result of the function.

===

= is used for assignment, == is used to determine whether the values ​​of the expressions on both sides are equal, === is used to determine whether the values ​​and types of the expressions on both sides are equal at the same time, because JavaScript is a weakly typed language. The type of a variable is not known when it is declared. It is recommended to always use === to determine whether the values ​​of expressions are equal.

new

new operator is used to create objects using constructors. For example: new Book(), or new Book, the following parentheses are not necessary. For details, please refer to JavaScript Constructor Principle.

Function call operator ()

Function name + () means executing the function. For example, var hello = function() {}, hello() executes the hello function.