Home Web Front-end JS Tutorial Summary of commonly used operators in JavaScript_javascript skills

Summary of commonly used operators in JavaScript_javascript skills

May 16, 2016 pm 05:56 PM
operator

1. Unary operators
1. delete operator: deletes references to previously defined object properties or methods. For example:
var o=new Object;
o.name="superman";
alert(o.name); //Output "superman"
delete o.name;
alert (o.name); //Output "undefined"
Remove the name attribute and set it to undefined (that is, the value of the uninitialized variable created). delete cannot delete properties and methods that are not defined by the developer (that is, defined by ECMAScript).
For example, the following code will cause an error: delete o.toString();
2. void operator: Returns undefined for any value. This operator is typically used to avoid outputting values ​​that should not be output.
For example: in the HTML page Click Me
Click the link, the link in the webpage disappears and the display "[object]" (in IE, "[object Window]" is displayed in Firefox, and the link is still the same in Google). Because the window.open() method returns a reference to the newly opened window. The object is then converted into a string to be displayed. To avoid this result, you can call the window.open() function with the void operator:
Click Me
This causes the window.open() call to return undefined, which is not a valid value and will not be displayed in the browser window.
3. Pre-increment/pre-decrement operator: The usage is the same as in C. Operations occur before expressions are evaluated. Such as: i; --i.
4. Post-increment/post-decrement operators: Postfix operators perform increment or decrement operations after calculating the expressions containing them. Such as: i; i--.
5. Unary addition and unary subtraction
Uniary addition essentially has no effect on numbers: var iNum=25; iNum= iNum; alert(iNum); //Output 25
Uniary addition operator pairs When operating on a string, the way it calculates the string is similar to parseInt(). The main difference is that for strings starting with "0x" (representing hexadecimal numbers), the unary operator can convert it into decimal. value. Therefore, converting "010" using unary addition will always result in 10, while "0xB" will be converted to 11.
var sNum="25"; alert(typeof sNum); //Output "string"
var iNum= sNum; alert(typeof iNum); //Output "number"
Uniary subtraction is logarithmic value Negative, similar to the unary addition operator, the unary subtraction operator will also convert a string into an approximate number, and will also negate the value.
var sNum = "25"; alert(typeof sNum); //Output "string"
var iNum = -sNum; alert(iNum); //Output "-25"
alert(typeof iNum ); //Output "number"
2. Bit operators: A series of operators related to binary, which seems to be rarely used in actual operations, so I skipped it... The NOT operator is represented by the negation sign (~). The bitwise operator AND is represented by the ampersand (&).
The bitwise operator OR is represented by the symbol (|).
The bitwise operator XOR is represented by the symbol (^).
The left shift operation is represented by two less than signs (<<).
The signed right shift operator is represented by two greater than signs (>>).
Unsigned right shift is represented by three greater than signs (>>>).

3. Boolean operators
There are three types of Boolean operators, namely NOT, AND and OR.
1. Logical NOT, represented by an exclamation point (!). This operator is typically used to control loops. Unlike the logical OR and logical AND operators, the logical NOT operator must return a Boolean value. The logical NOT operator behaves as follows:
If the operand is an object, returns false.
If the operand is the number 0, return true.
If the operand is any number other than 0, return false.
If the operand is null, return true.
If the operand is NaN, return true.
If the operand is undefined, an error occurs.
Example: var b = true;
while(!b){ …… }
2. Logical AND operator, represented by double ampersand (&&). There is only one situation when the result is true: true && true = true;
The operand of the logical AND operation can be of any type, not just Boolean values. If an operand is not a primitive Boolean value, the logical AND operation does not necessarily return a Boolean value:
If one operand is an object and the other is a Boolean value, the object is returned.
If both operands are objects, return the second object.
If an operand is null, return null.
If an operand is NaN, return NaN.
If an operand is undefined, an error occurs.
If the first operand is false, then no matter what the value of the second operand is, the result cannot be true.
3. Logical OR operator, represented by double vertical bars (||). There is only one case where the result is false: false || false=false; other cases are true.
Similar to the logical AND operator, if an operand is not a Boolean value, the logical OR operation does not necessarily return a Boolean value:
If one operand is an object and the other is a Boolean value, the object is returned.
If both operands are objects, return the first object.
If an operand is null, return null.
If an operand is NaN, return NaN.
If an operand is undefined, an error occurs.
Logical OR is also a simple operation. For the logical OR operator, if the first operand value is true, the second operand will not be calculated.
4. Multiplication operator
1. Multiplication operator, represented by an asterisk (*), is used to multiply two numbers. But when dealing with special values, multiplication in ECMAScript also has some special behaviors:
If the operands are all numbers, a regular multiplication operation is performed. If the result is too large or too small, the generated result is Infinity or -Infinity. .
If an operand is NaN, the result is NaN.
Infinity multiplied by 0, the result is NaN.
Infinity multiplied by any number other than 0, the result is Infinity or -Infinity, determined by the sign of the second operand.
2. The division operator, represented by a slash (/), divides the first number by the second number. For special values, the special behavior is as follows:
If the operands are all numbers, the normal trigger operation is performed. If the result is too large or too small, the generated result is Infinity or -Infinity.
If an operand is NaN, the result is NaN.
If 0 is divided by a non-infinity number, the result is NaN.
Infinity is divided by any number other than 0, and the result is Infinity or -Infinity, determined by the sign of the second operand.
3. Modulo operator, expressed with percent sign (%). If the operands are both numbers, perform regular arithmetic division and return the remainder. Special behavior:
If the dividend is Infinity, or the divisor is 0, the result is NaN.
If the divisor is an infinite number, the result is the dividend.
If the dividend is 0, the result is 0.
5. Additive operator
1. Addition operator ( ), special behavior:
A certain operand is NaN, and the result is NaN.
Infinity plus -Infinity, the result is NaN.
If one of the operands is a string, then the following rules apply:
If both operands are strings, concatenate the second string to the first string.
If only one operand is a string, convert the other operand into a string, and the result is a string concatenated by the two strings.
2. Subtraction operator (-), if both operands are numbers, arithmetic subtraction will be performed. Special rules:
If the operand is NaN, the result is NaN.
An operand is not a number, and the result is NaN.
6. Relational operators
The relational operators less than, greater than, less than or equal to, and greater than or equal to perform comparison operations between two numbers. The comparison method is the same as the arithmetic comparison operation. Each relational operator returns a Boolean value.
For strings, the code of each character in the first string is numerically compared to the code of the character at the corresponding position in the second string.
1. The codes of uppercase letters are smaller than the codes of lowercase letters, so to get the comparison result in true alphabetical order, the two operands must be converted into the same uppercase and lowercase forms and then compared.
2. When comparing two numbers in the form of strings, their character codes are compared.
3. When comparing a string and a number, ECMAScript will convert the string into a number and then compare them in numerical order. If the string cannot be converted to a number, NaN is returned, and any relational operation containing NaN must return false. Therefore, this returns false.
7. Equality operators
1. The equal sign (==) and the non-equal sign (!=). To determine whether two operands are equal, these two operators will Perform type conversion. The basic rules for performing type conversions are as follows:
If an operand is a Boolean value, convert it to a numeric value before checking for equality. False is converted to 0 and true is converted to 1.
If one operand is a string and the other is a number, try to convert the string to a number before checking for equality.
If one operand is an object and the other is a string, try to convert the object to a string (call the toString() method) before checking for equality.
If one operand is an object and the other is a number, try to convert the object to a number before checking for equality.
This operator also obeys the following rules when making comparisons:
The values ​​null and undefined are equal.
When checking equality, null and undefined cannot be converted to other values.
If an operand is NaN, the equal sign will return false, and the non-equal sign will return true. Even if both operands are NaN, the equal sign still returns false because according to the rules, NaN is not equal to NaN.
If both operands are objects, then their reference values ​​are compared. If the two operands point to the same object, then the equal sign returns true, otherwise the two operands are not equal.


2. Congruent and non-congruent signs
The same kind of operators of equal sign and non-congruent sign are congruent and non-congruent signs. These two operators do the same as the equal sign and the not equal sign, except that they do not perform a type conversion before checking for equality.
The congruent sign is represented by three equal signs (===), and returns true only if the operands are equal without type conversion.
var sNum="55"; var iNum=55; alert(sNum==iNum); //Output "true"
alert(sNum===iNum); //Output "false"
The non-identical sign is represented by two exclamation points plus two equal signs (!==), and returns true only if the operands are not equal without type conversion.
var sNum="55"; var iNum=55; alert(sNum != iNum); //Output "false"
alert(sNum !== iNum); //Output "true"
8. Other operators
1. Conditional operator, that is, ternary operator: variable = boolean_expression ? true_value : false_value;
2. Assignment operator (=) Compound assignment operator: =, -=, *=, /=, %=, <<=, >>=, >>>=
3. Comma operator The comma operator can be used in a statement Perform multiple operations. For example: var iNum1=1,iNum2=2;

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the root operator in C language? What is the root operator in C language? Mar 06, 2023 pm 02:39 PM

In the C language, there is no root operator. The built-in function "sqrt()" is used to open the root, and the syntax "sqrt(value x)" is used; for example, "sqrt(4)" is to perform the square root operation on 4. , the result is 2. sqrt() is a built-in root operation function in C language. Its operation result is the arithmetic square root of the function variable; this function can neither operate negative values ​​nor output imaginary results.

Golang error: 'invalid use of ... operator' How to solve it? Golang error: 'invalid use of ... operator' How to solve it? Jun 24, 2023 pm 05:54 PM

For Golang developers, "invaliduseof...operator" is a common error. This error usually occurs when using variable-length parameter functions. It will be detected at compile time and indicate which parts have problems. This article will introduce how to solve this error. 1. What is a variable-length parameter function? A variable-length parameter function is also called a variable-parameter function. It is a function type in the Golang language. Using variable-length parameter functions, you can define multiple ones as follows

What does % mean in Java What does % mean in Java Mar 06, 2023 pm 04:48 PM

In Java, "%" means remainder. It is a binary arithmetic operator that can perform division operations and obtain the remainder. The syntax is "operand 1 % operand 2". The operand of the remainder operator "%" is usually a positive integer or a negative number or even a floating point number. If a negative number participates in this operation, the result depends on whether the previous number is positive or negative.

Analysis of the meaning and usage of += operator in C language Analysis of the meaning and usage of += operator in C language Apr 03, 2024 pm 02:27 PM

The += operator is used to add the value of the left operand to the value of the right operand and assign the result to the left operand. It is suitable for numeric types and the left operand must be writable.

What is the meaning of '==' symbol in php What is the meaning of '==' symbol in php Mar 14, 2023 pm 07:05 PM

In PHP, the "==" symbol is a comparison operator that can compare whether two operands are equal. The syntax is "operand 1 == operand 2". The "==" operator compares and tests whether the variable on the left (expression or constant) has the same value as the variable on the right (expression or constant); it only compares the values ​​of the variables, not the data types. If the two values ​​are the same, it returns a true value; if the two values ​​are not the same, it returns a false value.

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

How to determine if two numbers are divisible in php How to determine if two numbers are divisible in php Jan 10, 2023 pm 03:12 PM

In PHP, you can use the "%" and "==" operators to determine whether two numbers are divisible; you only need to use the "%" operator to divide the two numbers to get the remainder, and then use the "==" operator Just judge whether the obtained remainder is 0. The syntax is "Number 1 % Number 2 == 0". If it is 0, it can be divisible. If it is not 0, it cannot be divisible.

Magic methods in Python Magic methods in Python Apr 13, 2023 am 10:25 AM

Magic methods in Python are special methods that allow you to add "magic" to a class. They are often named surrounded by two underscores. Python's magic method, also known as the dunder (double underline) method. Most of the time, we use them for simple things like constructors (init), string representations (str, repr) or arithmetic operators (add/mul). In fact, there are many methods that you may not have heard of but are very useful. In this article, we will sort out these magic methods! We all know the size of the iterator __len__ method, which can be used in container classes Implement the len() function on. However, if you want to get the length of a class object that implements iterator

See all articles