Home Web Front-end JS Tutorial Summary of JavaScript operator usage

Summary of JavaScript operator usage

Jul 29, 2017 pm 05:14 PM
javascript js operator

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

##--decrement. Decrement the operand by 1. n=--x;n=5 x=5##Comparison operator==##!=Not equal. If the two data are not equal, return true, otherwise return false. boolean2=(x!=5);boolean2=false;> is greater than. If the data on the left is greater than the data on the right, return true, otherwise return false. boolean4=(x>y);boolean4=true< is less than. If the data on the left is less than the data on the right, a Boolean value true is returned, otherwise false is returned. boolean5=(xboolean5=false>=Greater than or equal to. If the data on the left is greater than or equal to the data on the right, return true, otherwise return false. boolean6=(x>=y);boolean6=true<=Less than or equal to. If the data on the left is less than or equal to the data on the right, return true, otherwise return false. boolean7=(x<=y);boolean7=falseLogical operatorDescriptionExampleResult&&Logical AND. Returns true if the operands on both sides of the symbol are true, otherwise returns false. boolean_a=true&&false;boolean_a=false||Logical OR. If the operands on both sides of the symbol are false, return false, otherwise return true. boolean_b=true||false;boolean_b=true!Logical negation. Returns false if the operand to the right of the symbol is true, otherwise returns true. boolean_c=!true;boolean_c=false" + " sign can also be used to concatenate strings
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
Description Example Result
Equal. If the two data are equal, return true, otherwise return false. boolean1=(x==5); boolean1=true

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&#39;m  "+age+" ! ";
 alert(person);
Copy after login

Save and run the code to display My name is Tom! I'm 22!

In the above example , including strings and numerical values. When strings and numerical values ​​are mixed, JavaScript will automatically determine whether the "+" sign is used for addition or concatenation of strings. If concatenating strings, the numeric value will also be converted to a string.

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

Save and run the code, click on the four pieces of text in sequence, and all of them will display 6.

Analysis:

For y, the value of x (x=5) plus 1 becomes 6, and then the value of x is passed to y.

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 operators

OperatorExample is equivalent to +=x+=yx=x+y-=x-=yx=x-y*=x*=yx=x*y/=x/=yx=x/y%=x%=yx=x%y

The above is the entire content of this article, I hope you all like it.


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!

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)

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

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

Analysis of the meaning and usage of += operator in C language Analysis of the meaning and usage of += operator in C language Apr 03, 2024 pm 02:27 PM

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.

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

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

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

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

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 The relationship between js and vue Mar 11, 2024 pm 05:21 PM

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.

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

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

Python Operators: The Ultimate Guide from Newbie to Master Python Operators: The Ultimate Guide from Newbie to Master Mar 11, 2024 am 09:13 AM

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

See all articles