Detailed explanation of expressions and operators in JavaScript
This article mainly introduces JavaScript expressions and operators. Friends in need can refer to it
JavaScript scripting language describes a set of operators used to operate data values, including unary operators. Boolean operators, arithmetic operators, relational operators, ternary operators, bitwise operators and assignment operators.
An expression is a "phrase" in the JavaScript language, including variable names (or literals) and operators. The simplest expressions are literals or variable names. Of course there are also ways to combine simple expressions to create complex expressions.
1. Unary operators
(1)increment++ and decrement--
var box1=100; ++box1;//相当于box=box+1 document.write("box1="+box1+"<br/>");//输出box1=101 var box2=100; --box2;//相当于box=box2-1 document.write("box2="+box2);//输出box2=99
The difference between prefix and postfix
var box=100; var age=++box;//box先累加1为101,再赋值给age为101 var height=box++;//box先赋值给height为101,box再累加为102 document.write("age="+age+"<br/>");//输出age=101 document.write("height="+height+"<br/>");//输出height=101 document.write("box="+box);//输出box=102,原因是box经过了两次累加,所以是102
In the absence of assignment operation, prefix and postfix are the same. However, during the assignment operation, if the increment or decrement operator is preceded, the preceding operator will be accumulated or decremented first and then assigned. If it is a postpositioned operator, the value will be assigned first and then accumulated or decremented.
(2) Addition and subtraction operators
are used for positive or negative operations, and also have the function of converting numeric strings into numerical forms.
var box = "20"; document.write(typeof box+"<br/>"); //输出string var age=-box; document.write(age+"<br/>");//输出-20 document.write(typeof age); //输出number
2. Arithmetic operators
There are five arithmetic operators specified in JavaScript language, namely +, -, * , / and % (remainder). If the value in an arithmetic operator is not a numeric value, then it will first be converted to a numeric value using the Number() conversion function (implicit conversion).
var box=100+"100"; document.write("box="+box+"<br/>");//输出100100 document.write(typeof box);//输出string
Why is this? When doing arithmetic operations in the JavaScript language, as long as one of them is a string, the result will be converted to a string. Equivalent to the string concatenation operator and can no longer be counted as an addition arithmetic operator.
var box="100"-10; document.write("box="+box+"<br/>");//输出90 var age=5/4; document.write("age="+age+"<br/>");//输出1.25 var height=("你的年龄是:"+(10+10));//括号强制优先级 document.write(height);//输出你的年龄是:20
Remainder
var box=10%3; document.write("box="+box);//输出1
3. Relational operators
The operators used for comparison are called relational operators: < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == (relative) , != (not equal), === (identical or congruent), !== (not congruent or not identical). Most relational operators return a Boolean value.
is the same as other operators. When the non -numbers of the operation of the relationship operator are operated, the following rules must be followed:
1 The two operators are values, then the value comparison of the values is
2 2 operations are string. Then compare the character encoding values corresponding to the two strings
3If one of the two operands is a numeric value, convert the other into a numeric value, and perform the numeric comparison
4If one of the two operands is an object, then First call the value() method or toString() method, and then compare the results.
var box1=3>2; document.write(box1+"<br/>");//输出true var box2="3">22; document.write(box2+"<br/>");//输出false var box3="3">"22"; document.write(box3+"<br/>");//输出true var box4="a">"B";//a为97,B为66 document.write(box4+"<br/>");//输出true var box5= "Blue"<"alpha";//Blue的第一个字母是B,alpha的第一个字母是a,a为97,B为66 document.write(box5) //输出true
In equality and inequality comparisons, if the operand is a non-numeric value, the following rules apply:
1 One operand is a Boolean value, Then convert it to a numerical value before comparison, false is converted to 0, and true is converted to 1.
2If one operand is a string, it will be converted into a numerical value before comparison.
3. If one operand is an object, call the value() method or toString() method first and then compare.
4 Without any conversion, null and undefined are equal
5 If one operand is NaN, then == returns false, != returns true, and NaN is not equal to itself
6 Two If both operands are objects, compare them to see if they are the same object. If they all point to the same object, return true, otherwise return false Returns true only if they are equal, otherwise returns fasle.
var box1='2'==2; document.write(box1+"<br/>");//输出true,比较的只是数值 var box2={}=={}; document.write(box2+"<br/>");//输出false,因为比较的是它们的地址,每个新创建对象的引用地址都不同。 var box3=null==undefined; document.write(box3+"<br/>");//输出true,因为均为空数值 var box4='2'===2; document.write(box4+"<br/>");//输出false,两个操作数的数据类型不相等 var box5=null===undefined; document.write(box5);//输出false,两个操作数的数据类型不相等
四逻辑运算符
JavaScript语言中的逻辑运算符通常作用于布尔值的操作,一般和关系运算符配合使用,有三个逻辑运算符:&&(逻辑与),||(逻辑或)和!(逻辑非)。
(1)&&表示两边都必须是true,才返回true。
如果两边的操作数有一个操作数不是布尔值的情况下,与运算就不一定返回布尔值,此时遵循下面的规则:
1第一个操作数是对象,则返回第二个操作数
2第二操作数是对象,则第一个操作数返回true,才返回第二个操作数,否则返回false
3一个操作数是null,则返回null
4一个操作数是undefined,则返回undefined
5如果一个运算数是对象,另一个是 Boolean 值,返回该对象
逻辑与运算符属于短路操作,如果有第一个操作数返回的是false,第二个不管是true还是false都返回false。
var box1={}&&(5>4); document.write(box1+"<br/>");//输出true var box2=(5>4)&&{}; document.write(box2+"<br/>");//输出[object Object] var box3=(3>4)&&{}; document.write(box3);//输出false
(2)||表示两边有一个是true,就返回true。
如果两边的操作数有一个操作数不是布尔值的情况下,与运算就不一定返回布尔值,此时遵循下面的规则:
1第一个操作数是对象,则返回第一个个操作数
2第一个操作数的求值结果为fasle,则返回第二个操作数
3两个操作数都是对象,则返回第一个操作数
4两个操作数都是null,则返回null
5两个操作数都是undefined,则返回undefined
6两个操作数都是NaN,则返回NaN
逻辑或运算符也属于短路操作,如果有第一个操作数返回的是true,第二个不管是true还是false都返回true。
var box1={}||(5>4); document.write(box1+"<br/>");//输出[object Object] var box2=(5>4)||{}; document.write(box2+"<br/>");//输出true var box3=(3>4)||{}; document.write(box3);//输出[object Object]
(3)!逻辑非运算符可以作用与任何值,无论这个值是什么数据类型,这个运算符都会返回一个布尔值,它的流程是:先将这个值转换成布尔值,然后取反,规则如下:
1操作数是一个对象,返回false
2操作数是一个空字符串,返回true
3操作数是一个非空字符串,返回false
4操作数是数值0,返回true
5操作数是任意非0数值,返回false
6操作数是null,返回true
7操作数是NaN,返回true
8操作数是undefined,返回true
var box=!{}; document.write(box);//输出false
五、位运算符
JavaScript语言中包括了七种位运算符:~(位非),&(位与),|(位或),^(位异或),<<(左移),>>(有符右移号),>>>(无符号右移)
(1)位非(~)运算把运算数转换成32位数字,然后把二进制数转换成它的二进制反码,最后把二进制数转换成浮点数。实质上是对数字求负,然后减去1即为所得值。
var box=~25; document.write(box);//输出-26
(2)位与(&)运算直接对数字的二进制形式进行运算,然后对上下同一位置的两个数位进行与运算,只有两个数位都为1时才得出1,其余的均为0.
var box=25&3; document.write(box);//输出1
(3)位或(|)运算也是直接对数字的二进制形式进行计算,然后对上下同一位置的两个数位进行或运算,只右两个数位都为0时才得出0,其余的均为1.
var box=25|3; document.write(box);//输出27
(4)位异或(^)也是直接对二进制形式进行运算。当只有一个数位存放的是1时,它才返回1。其余的返回0。也就是两个数位相同时返回0,不同时返回1.
var box=25^3; document.write(box);//输出26
(5)左移运算也是对二进制数进行操作,相等于第一个操作数乘以(2的左移位数次幂)的积。
var box=25<<3; document.write(box);//25左移3位相当于25乘以(2的3次幂),因此输出200
(6)有符号右移运算也是对二进制数进行操作,相等于第一个操作数除以(2的右移位数次幂)的商。
var box=24>>2; document.write(box);//输出6
(7)无符号右移运算也是对二进制数进行操作,对于正数,与有符号右移是相同的结果,但是对于负数,就会所不同。
六、赋值运算符
赋值运算符包括:=(),+=(),-=(),*=(),/=(),%=(),<<=(),>>=(),>>>=()。
var box=100; box+=100;//相当于box=box+100 document.write("box="+box);//输出box=200
七、其他运算符
1)、字符串运算符:“+”,它的作用是将两个字符串想加。规则:只要有一个字符串即可。
var box=100+"10"'; document.write("box="+box);//输出100100
2)、逗号运算符,可以在一条语句中执行多个操作
var box=100,age=200,height=300; document.write("box="+box);//输出box=100
3)、三元操作符:
var box=(3>4)?"对":"错"; document.write(box);//输出错
如果想更详细的了解ECMAScript运算符的知识,可以访问JavaScript高级教程中的ECMASscript一元运算符这个系列中有详细的运算符教程。对于JS的运算符来说,我们可以对比着C++,C#和Java来学,这个还是相当的容易的。
以上就是关于JavaScript的表达式与运算符的全部内容,希望对大家的学习有所帮助。
The above is the detailed content of Detailed explanation of expressions and operators in JavaScript. 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

AI Hentai Generator
Generate AI Hentai for free.

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

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

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

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

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

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.

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

1. Introduction to Python Python is a general-purpose programming language that is easy to learn and powerful. It was created by Guido van Rossum in 1991. Python's design philosophy emphasizes code readability and provides developers with rich libraries and tools to help them build various applications quickly and efficiently. 2. Python basic syntax The basic syntax of Python is similar to other programming languages, including variables, data types, operators, control flow statements, etc. Variables are used to store data. Data types define the data types that variables can store. Operators are used to perform various operations on data. Control flow statements are used to control the execution flow of the program. 3.Python data types in Python

In Go language, operators are evaluated in order from high to low precedence. The priority order of common operators: 1. Parentheses: () (highest priority, used to force the order of operations); 2. Unary operators; 3. Multiplicative operators; 4. Additive operators; 5. Shift operator; 6. Bitwise operator; 7. Comparison operator; 8. Logical operator; 9. Conditional operator (ternary operator); 10. Assignment operator, etc.
