In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "what is the javascript arithmetic operator". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In javascript, an arithmetic operator is a symbol used to perform arithmetic operations on numbers (literals or variables), including the addition operator "+", the subtraction operator "-", the multiplication operator "*", the division operator "/", the remainder operator "%", and the numeric inverse operator "-".
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
In javascript, an arithmetic operator is a symbol used to perform an arithmetic operation on a number (text or variable); it is a symbol that performs a basic arithmetic operation (arithmetic operators), a symbol used to handle four operations.
The arithmetic operators include: addition +, subtraction -, multiplication *, division /, remainder operator%, numeric inverse operator -.
Operator description example + addition operator x + y for sum-subtraction operator x-y for calculation x minus y * multiplication operator x * y for product / division operator x / y for calculation x divided by y quotient% modulus (remainder) operator x% y represents the remainder of calculation x divided by y
Addition operator
Example 1
Pay attention to the summation of special operands.
Var n = 5; / / define and initialize any numeric console.log (NaN + n); / / add NaN to any Operand, the result is NaNconsole.log (Infinity + n); / / Infinity plus any Operand, the result is Infinityconsole.log (Infinity + Infinity); / / Infinity and Infinity add, the result is Infinityconsole.log ((- Infinity) + (- Infinity); / / negative Infinity addition, the result is negative Infinityconsole.log ((- Infinity) + Infinity) / / plus or minus Infinity, the result is NaN
Example 2
The addition operator can determine whether to add or join operations based on the data type of the operands.
Console.log (1 + 1); / / if all operands are numeric, add console.log (1 + "1"); / / if one of the operands is a string, perform concatenation operation console.log (3.04.3 + "); / / sum first, then concatenate, return" 7.3" console.log (3.0 + "" + 4.3) / / concatenate first, then connect, return "34.3" / / 3.0 converted to string 3
When using the addition operator, you should first check that the data type of the Operand meets the needs.
Subtraction operator
Example 1
Pay attention to the subtraction of special operands.
Var n = 5; / / define and initialize any numeric console.log (NaN-n); / / NaN minus any Operand, the result is NaNconsole.log (Infinity-n); / / Infinity minus any Operand, the result is Infinityconsole.log (Infinity-Infinity); / / Infinity subtracts from Infinity, and the result is NaNconsole.log ((- Infinity)-(- Infinity)) / / minus Infinity subtracts, the result is NaNconsole.log ((- Infinity)-Infinity); / / positive and negative Infinity subtracts, the result is-Infinity
Example 2
In subtraction, if the Operand is a string, try to convert it to a numeric value before performing the operation. Returns NaN if one of the operands is not a number.
Console.log (2-"1"); / / return 1console.log (2-"a"); / / return NaN
You can quickly convert a value to a number by subtracting 0 from the value. For example, query strings in HTTP requests are generally string numbers, so you can first subtract 0 from these parameter values and convert them to numeric values. This is the same as calling the parseFloat () method, but subtraction is more efficient and faster. If the implicit conversion of the subtraction operator fails, it returns NaN, which is different from the return value when the conversion is performed using the parseFloat () method.
For example, for the string "100aaa", the parseFloat () method parses the first few numbers, while for the subtraction operator, it must be a complete number before it can be converted.
Console.log (parseFloat ("100aaa")); / / return 100console.log ("100aaa"-0); / / return NaN
For Boolean values, the parseFloat () method converts true to 1, false to 0, and the subtraction operator treats it as NaN.
For an object, the parseFloat () method attempts to call the object's toString () method for conversion, while the subtraction operator first attempts to call the object's valueOf () method for conversion, and then calls toString () for conversion after failure.
Inversion operation
The inverse operator is a unary operator, also known as a unary subtraction operator.
Example
Pay attention to the inverse operation of special operands.
Console.log (- 5); / / returns-5. The normal value takes a negative number console.log (- "5"); / / returns-5. First convert the string number to the numeric type console.log (- "a"); / / return NaN. Cannot exactly match the operation, return NaNconsole.log (- Infinity); / return-Infinityconsole.log (- (- Infinity)); / / return Infinityconsole.log (- NaN); / / return NaN
Corresponding to the unary subtraction operator is the unary addition operator, which can be used to quickly convert a value into a numerical value.
Multiplication operator
Pay attention to the multiplication of special operands.
Var n = 5; / / define and initialize any numeric console.log (NaN * n); / / NaN multiplies any Operand, the result is NaNconsole.log (Infinity * n); / / Infinity multiplies any non-zero positive number, the result is Infinityconsole.log (Infinity * (- n)); / / Infinity is multiplied by any non-zero negative number, the result is-Infinityconsole.log (Infinity * 0) / / Infinity multiplied by 0, the result is NaNconsole.log (Infinity * Infinity); / / Infinity multiplied by Infinity, the result is Infinity
Division operator
Pay attention to the division of special operands.
Var n = 5; / / define and initialize any numeric console.log (NaN / n); / / if one Operand is NaN, the result is NaNconsole.log (Infinity / n); / / Infinity is divided by any number, and the result is Infinity or-Infinity / / symbol determined by the symbol of the second Operand console.log (Infinity / Infinity); / / returns NaNconsole.log (n / 0) / / 0 divided by a non-infinite number, the result is Infinity or-Infinity, the symbol is determined by the symbol of the second Operand console.log (n /-0); / / returns-Infinity, interpreted as above
Remainder operator
Congruence operations are also called modular operations, for example:
Console.log (3% 2); / / returns the remainder 1
Modular operations mainly operate on integers and are also suitable for floating-point numbers. For example:
Console.log (3.1% 2.3); / / returns the remainder 0.800000000003
Example
Pay attention to the remainder operation of special operands.
Var n = 5; / / define and initialize any numeric value console.log (Infinity% n); / / return NaNconsole.log (Infinity% Infinity); / / return NaNconsole.log (n% Infinity); / / return 5console.log (0% n); / / return 0console.log (0% Infinity); / / return 0console.log (n% 0); / / return NaNconsole.log (Infinity 0) / / return to NaN, "what is the javascript arithmetic operator?" that's it. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.