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/03 Report--
This article mainly introduces "how to achieve arithmetic addition in javascript". In daily operation, I believe many people have doubts about how to achieve arithmetic addition in javascript. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to achieve arithmetic addition in javascript". Next, please follow the editor to study!
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
How to realize addition Operation by javascript
Method 1: use the "+" 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 decide whether to add or join operations based on the data type of the operands.
Console.log (1 + 1); / / add console.log (1 + "1") if all operands are numeric; / / perform concatenation operation console.log (3.04.3 + ") if one of the operands is a string; / / sum first, then concatenate, and return" 7.3" console.log (3.0 + "" + 4.3) / / concatenate first, then connect, return "34.3" / / 3.0 converted to string 3
Note: when using the addition operator, you should first check whether the data type of the Operand meets the needs.
Method 2: use the "+ =" operator
The role of the "+ =" operator: perform an addition or join operation on the right Operand before assigning, and then copy the result to the left Operand
Var axiom 1, console.log 2, console.log (c), console.log (d), etc.
Method 3: use the "+" increment operator
The incremental + + operation is a simple way to change its own results by constantly adding 1 and then assigning the result to the left Operand.
As a unary operator, the increment operator function acts on variables, array elements, or object attributes, not on direct quantities. According to the location, it can be divided into two kinds of operation:
Pre-increment (+ + n): increment first and then assign a value.
Post increment (nincrement +): assign a value first, and then increment it.
Example:
Var a = b = c = 4 console.log (aquired +); / / return 4, assign first, then increment the result unchanged console.log (+ + b); / / return 5, increment first, then assign, add 1console.log (C++); / / return 4, assign first, then increment, the result remains unchanged console.log (c); / / return 5, add 1console.log (+ + c) to the value of the variable / / returns 6, increments first, then assigns values, and adds 1console.log (c) to the result; / / returns 6
At this point, the study of "how to achieve arithmetic addition in javascript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.