Table of Contents
Addition operator
Example:
Subtraction operator
Multiplication operator
Division operator
Remainder operator
Increment operator
递减运算符
幂运算符
示例:
Home Web Front-end JS Tutorial What are the javascript arithmetic operators?

What are the javascript arithmetic operators?

Jul 16, 2021 am 11:22 AM
javascript arithmetic operators

Javascript arithmetic operators include: addition operator " ", subtraction operator "-", multiplication operator "*", division operator "/", remainder operator "%", increment operator " ", decrement operator "--", power operator "**".

What are the javascript arithmetic operators?

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

那为什么结果会是6呢,i++ 其实就是表示在 i 的基础上加一,相当于i + 1

然后我们看一下递增运算符前置和后置,到底有什么区别,例如下面这个代码:

var a = 9;
console.log(a++);  // 输出:9

console.log(a);    // 输出:10

console.log(++a);  // 输出:11
Copy after login
  • 变量 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
Copy after login
  • 变量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
Copy after login

上面的运算出的结果与 Math.pow(x, y) 是相同的,例如:

var x = 6;
var y = Math.pow(x,3);
console.log(y);  // 216
Copy after login

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!

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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles