Summary of JavaScript operator usage
This article mainly gives you a detailed summary of the operators in JavaScript, including common arithmetic operators, comparison operators and logical operators. It is very clear, friends who need it can refer to it.
In JavaScript, common operators include arithmetic operators, comparison operators and logical operators.
Table 1 JavaScript common operators
Arithmetic operators | Explanation | Examples | Result |
---|---|---|---|
= | Assignment operator. Assign the value of the variable on the right side of the operator to the variable on the left. | x = 5; | - |
+ | plus sign. Add two numbers. | y=1+2; | y=3 |
- | Minus sign. Subtract two numbers. | z = x-y; | z=2 |
* | Multiply sign. Multiply two data together. | a=x*y; | a=15 |
/ | Division sign. Divide two data. | b=x/z; | b=2.5 |
% | Remainder operation. Find the remainder after dividing two numbers. | c=x%z; | c=1 |
++ | Self-added. Add 1 to the operand. | m=++x; | m=6 x=6 |
decrement. Decrement the operand by 1. | n=--x; | n=5 x=5 | |
Description | Example | Result | |
Equal. If the two data are equal, return true, otherwise return false. | boolean1=(x==5); | boolean1=true | ##!= |
boolean2=(x!=5); | boolean2=false; | > | |
boolean4=(x>y); | boolean4=true | < | |
boolean5=(x | >= | ||
boolean6=(x>=y); | boolean6=true | <= | |
boolean7=(x<=y); | boolean7=false | Logical operator | |
Example | Result | && | |
boolean_a=true&&false; | boolean_a=false | || | |
boolean_b=true||false; | boolean_b=true | ! | |
boolean_c=!true; | boolean_c=false |
The "+" sign can not only add two data, but can also be used to connect strings.
For example: The code is as follows:var name=" Tom "; var age=22; var person="My name is "+name+" ! I'm "+age+" ! "; alert(person);
Discussion on self-increasing (++) and self-decreasing (--)
It is worth noting that self-increasing (++) and self-decreasing (- -) The operator has different meanings when placed before and after the operand. If it is placed in front of the operand (front self-increment/front self-decrement), the operand will be added by 1 (subtracted by 1) first, and then the operation will be performed; if it is placed after the operand (last self-increment/back self-decrement), the operation will be performed first. Then add 1 to the operand (decrease 1).
For example: The code is as follows:<script type="text/javascript"> var x=5; var y=++x; // 前自加,赋值后 x 的值为 6 var z=x++; // 后自加,赋值后 x 的值为 7 var m=--x // 前自减,赋值后 x 的值为 6 var n=x-- // 后自减,赋值后 x 的值为 5 </script> <p onclick="alert(y);">显示 y 的值</p> <p onclick="alert(z);">显示 z 的值</p> <p onclick="alert(m);">显示m 的值</p> <p onclick="alert(n);">显示 n 的值</p>
Analysis:
For z, first pass the value of x (x=6) to z, then add 1 to x, and the value becomes 7.
For m, the value of x (x=7) minus 1 is 6, and then the value of x is passed to m.For n, first pass the value of x (x=6) to n, then subtract 1 from x, and the value becomes 5.
Abbreviations of arithmetic operators
In order to facilitate operation and reduce code writing, JavaScript also supports abbreviations of common mathematical operators.
Table 2 Abbreviations of common arithmetic operatorsis equivalent to | += | |
---|---|---|
x=x+y | -= | |
x=x-y | *= | |
x=x*y | /= | |
x=x/y | %= | |
x=x%y |
The above is the detailed content of Summary of JavaScript operator usage. For more information, please follow other related articles on the PHP Chinese website!

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

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

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

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.

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

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

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

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Introduction to python operators Operators are special symbols or keywords used to perform operations between two or more operands. Python provides a variety of operators covering a wide range of uses, from basic mathematical operations to complex data manipulation. Mathematical operators Mathematical operators are used to perform common mathematical operations. They include: operator operation examples + addition a + b - subtraction a-b * multiplication a * b / division a / b % modulo operation (take the remainder) a % b ** power operation a ** b // integer division (discard the remainder) a//b Logical Operators Logical operators are used to concatenate Boolean values and evaluate conditions. They include: operator operations examples and logical and aandbor logical or aorbnot logical not nota comparison operations
