Javascript arithmetic operators include: addition operator " ", subtraction operator "-", multiplication operator "*", division operator "/", remainder operator "%", increment operator " ", decrement operator "--", power operator "**".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Arithmetic operators are used to perform arithmetic operations on numbers (literal or variables). The standard arithmetic operators are addition
, subtraction -
, multiplication *
, and division /
. It is very similar to addition, subtraction, multiplication and division in mathematics. Let’s take a look at it together.
Addition operator
Addition operator
is used to sum the values, which should be very simple.
Example:
The following is a simple sum of two numbers:
var a = 8; var b = a + 5; console.log(b); // 13
In addition to calculating the sum of two numbers, we can activate the operator by
Concatenate strings and the result is a new string.
Example:
Use
to connect the three defined variables:
str1 = "hello"; str2 = "xkd"; str3 = " "; console.log(str1 + str3 + str2); // 输出: hello xkd
In addition, the numbers and A string can also be connected by
plus sign, and the returned result is also a string.
Example:
Look at the difference between adding numbers and numbers, and adding numbers and strings:
num1 = 10; num2 = 15; str1 = "15" console.log(num1 + num2 ); // 输出: 25 console.log(num1 + str1 ); // 输出:1015
Note that in some programming Different types in languages (such as Python) cannot be added. For example, adding numbers to strings will result in an error. In JavaScript, numbers and strings are added and a string is returned.
Subtraction operator
Subtraction operator -
can be used to subtract two operands, and the result is their difference.
Example:
var c = 8; var d = c - 5; console.log(d); // 3
In the subtraction operation, if the operand is a string, try to convert it to a numerical value before performing the operation. If one of the operands is not a number, NaN is returned.
Example:
console.log(2 - "1"); //返回1 console.log(2 - "a"); //返回NaN
Quickly convert a value to a number by subtracting 0 from the value. For example, query strings in HTTP requests are generally string numbers. You can first subtract 0 from these parameter values to convert them into numerical values. This has the same result as calling the parseFloat() method, but the subtraction is more efficient and faster. Implicit conversions with the subtraction operator return NaN if they fail, which is different from the return value when performing the conversion using the parseFloat() method.
For example, for the string "100aaa", the parseFloat() method can parse out the first few numbers, but for the subtraction operator, it must be a complete number before it can be converted.
console.log(parseFloat("100aaa")); //返回100 console.log("100aaa" - 0); //返回NaN
For Boolean values, the parseFloat() method can convert true to 1 and false to 0, while the subtraction operator treats it as NaN.
For objects, the parseFloat() method will try to call the object's toString() method for conversion, while the subtraction operator first tries to call the object's valueOf() method for conversion, and then calls toString() after failure. Make the conversion.
Pay attention to the subtraction operation of special operands.
var n = 5; //定义并初始化任意一个数值 console.log(NaN - n); //NaN与任意操作数相减,结果都是NaN console.log(Infinity - n); //Infinity与任意操作数相减,结果都是Infinity console.log(Infinity - Infinity); //Infinity与Infinity相减,结果是NaN console.log((-Infinity) - (-Infinity)); //负Infinity相减,结果是NaN console.log((-Infinity) - Infinity); //正负Infinity相减,结果是-Infinity
Inversion operation
Pay attention to the inversion operation of special operands
console.log(- 5); //返回-5。正常数值取负数 console.log(- "5"); //返回-5。先转换字符串数字为数值类型 console.log(- "a"); //返回NaN。无法完全匹配运算,返回NaN console.log(- Infinity); //返回-Infinity console.log(- (- Infinity)); //返回Infinity console.log(- NaN); //返回NaN
Multiplication operator
The result of the multiplication operator*
is the product of the operands.
Example:
var e = (8 + 5) * 3; var f = 'xkd' * 3; console.log(e); // 输出:39 console.log(f); // 输出:NaN
If we use a string to multiply a number, it will eventually return a NaN, which is an illegal number.
Division operator
The result of division operator/
is the quotient of the operands. The left operand is the dividend and the right operand is the divisor. .
Example:
var g = (9 - 3) / 3; var h = 3.0 / 1.0; var i = 1 / 2; console.log(g); //输出:2 console.log(h); //输出:3 console.log(i); //输出:0.5
What we need to pay attention to is that in JavaScript, 1 / 2
This kind of operation with a decimal point will result in Decimal point, for example 0.5
. In languages such as Java, numbers are not required to be clear floating-point numbers, and the return result of 1 / 2
is 0.
Remainder operator
Percent sign%
is the remainder operator, returning the first operand to the second operand Modulo (remainder), for example x % y
, the result is the integer remainder of x
divided by y
. Everyone should know that the remainder, which we have also learned in mathematics, refers to the part of the dividend that is not divided by the integer trigger.
Example:
For example, the following code:
var m = 9; var n = 2; var mn = m % n; console.log(mn); //输出: 1
The output result is 1, which is actually easy to understand, 9 % 2
Just find the remainder of 9 divided by 2, which is 1.
So if it is 12 % 5
, what will the output be? Dividing 12 by 5 leaves a remainder of 2, so the result is 2. Now you should know how to use %
.
Increment operator
Increment operator
Increases its operand by 1 and returns a numeric value. If you use a postfix, such as x
, the value will be returned before incrementing. If preceded, such as x
, the value will be returned after incrementing.
Example:
假设我们定义了一个变量 i
,然后使用自增运算符对 i
进行递增运算,将递增后的 i
赋值给了变量 j
,最终j
的输出结果为 6:
var i = 5; i++; var j = i; console.log(j); // 6
那为什么结果会是6呢,i++
其实就是表示在 i
的基础上加一,相当于i + 1
。
然后我们看一下递增运算符前置和后置,到底有什么区别,例如下面这个代码:
var a = 9; console.log(a++); // 输出:9 console.log(a); // 输出:10 console.log(++a); // 输出:11
- 变量 a 的值为9,然后使用后置递增运算符
a++
,第一次输出会在递增之前就返回数值,即输出结果还是 9。 - 然后此时输出 a 的值,可以看到 a 的值已经为10了,因为已经执行了一次递增运算符,所以加 1。
- 接着第三次输出时,使用前置递增运算符,这会在递增之后才返回数值,即输出结果为11。
递减运算符
递减运算符 --
为其操作数减去1,并返回一个数值。递减运算符和递增运算符的使用方法差不多,一个是减、一个是加,正好相反。
如果后置使用递减运算符,则在递减之前返回数值。如果前置使用,则在递减之后返回数值。
示例:
var b = 7; console.log(b--); // 输出:7 console.log(b); // 输出:6 console.log(--b); // 输出:5
- 变量b的值为7,然后使用后置递减运算符
b--
,会在递减之钱返回数值,即7。 - 然后第二次输出变量b,此时已经成功执行
b--
,会在此基础上减1,所以输出6。 - 第三次输出
--b
,使用后置递减运算符,会在递减之后返回数值,所以会输出5。
幂运算符
幂运算符 **
返回第一个操作数做底数,第二个操作数做指数的乘方。例如5 ** 2
表示 5 的 2 次方,根据所学数学知道就能得出结果为25。
示例:
下面这个代码表示求 6 的 3 次方,相当于 6 * 6 * 6
,结果为216:
var x = 6; var y = x ** 3; console.log(y); // 216
上面的运算出的结果与 Math.pow(x, y)
是相同的,例如:
var x = 6; var y = Math.pow(x,3); console.log(y); // 216
pow()
方法可返回 x 的 y 次幂的值。
【推荐学习:javascript高级教程】
The above is the detailed content of What are the javascript arithmetic operators?. For more information, please follow other related articles on the PHP Chinese website!

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor