Home Web Front-end JS Tutorial Detailed explanation of JavaScript expressions and operators_javascript skills

Detailed explanation of JavaScript expressions and operators_javascript skills

May 16, 2016 pm 03:29 PM
javascript expression operator

JavaScript scripting language describes a set of operators for operating 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 a variable name (or literal) and an operator. The simplest expressions are literals or variable names. Of course there are also ways to combine simple expressions to create complex expressions.
1. Unary operator
(1) Increasing and decreasing--

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

The difference between front and rear

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

When there is no 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
is used for positive or negative operations, and also has 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 
Copy after login

2. Arithmetic operators
There are five arithmetic operators specified in the 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 
Copy after login

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

Get the remainder

var box=10%3; 
document.write("box="+box);//输出1 
Copy after login

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.
Like other operators, the following rules must be followed when relational operators operate on non-numeric values:
​ ​ 1Both operators are numerical values, then numerical comparison
​ ​ 2If both operands are strings, compare the character encoding values ​​corresponding to the two strings
​ ​ 3If one of the two operands is a numerical value, convert the other into a numerical value and perform numerical comparison
​ ​ 4If one of the two operands is an object, call the value() method or toString() method first, 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 
Copy after login

In equality and inequality comparisons, if the operand is a non-numeric value, the following rules apply:
​ ​ 1If an operand is a Boolean value, it is converted 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 and then compared.
​ ​ 3If an operand is an object, call the value() method or toString() method first and then compare.
​ ​ 4 Without any conversion, null and undefined are equal
​ ​ 5If an operand is NaN, then == returns false, != returns true, and NaN is not equal to itself
​ ​ 6If both operands are objects, compare whether they are the same object. If they both point to the same object, return true, otherwise return false
​ ​ 7In the judgment of congruence and congruence, for example, if the values ​​​​and types are equal, true will be returned, otherwise fasle will be returned.

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,两个操作数的数据类型不相等 
Copy after login

四逻辑运算符
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 
Copy after login

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

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

五、位运算符
JavaScript语言中包括了七种位运算符:~(位非),&(位与),|(位或),^(位异或),<<(左移),>>(有符右移号),>>>(无符号右移)
(1)位非(~)运算把运算数转换成32位数字,然后把二进制数转换成它的二进制反码,最后把二进制数转换成浮点数。实质上是对数字求负,然后减去1即为所得值。

var box=~25; 
document.write(box);//输出-26 
Copy after login

(2)位与(&)运算直接对数字的二进制形式进行运算,然后对上下同一位置的两个数位进行与运算,只有两个数位都为1时才得出1,其余的均为0.

var box=25&3; 
document.write(box);//输出1 
Copy after login

(3)位或(|)运算也是直接对数字的二进制形式进行计算,然后对上下同一位置的两个数位进行或运算,只右两个数位都为0时才得出0,其余的均为1.

var box=25|3; 
document.write(box);//输出27 
Copy after login

(4)位异或(^)也是直接对二进制形式进行运算。当只有一个数位存放的是1时,它才返回1。其余的返回0。也就是两个数位相同时返回0,不同时返回1.

var box=25^3; 
document.write(box);//输出26 
Copy after login

(5)左移运算也是对二进制数进行操作,相等于第一个操作数乘以(2的左移位数次幂)的积。

var box=25<<3; 
document.write(box);//25左移3位相当于25乘以(2的3次幂),因此输出200 
Copy after login

(6)有符号右移运算也是对二进制数进行操作,相等于第一个操作数除以(2的右移位数次幂)的商。

var box=24>>2; 
document.write(box);//输出6 
Copy after login

(7)无符号右移运算也是对二进制数进行操作,对于正数,与有符号右移是相同的结果,但是对于负数,就会所不同。

六、赋值运算符
赋值运算符包括:=(),+=(),-=(),*=(),/=(),%=(),<<=(),>>=(),>>>=()。

var box=100; 
box+=100;//相当于box=box+100 
document.write("box="+box);//输出box=200 
Copy after login

七、其他运算符
1)、字符串运算符:“+”,它的作用是将两个字符串想加。规则:只要有一个字符串即可。

var box=100+"10"'; 
document.write("box="+box);//输出100100 
Copy after login

2)、逗号运算符,可以在一条语句中执行多个操作

var box=100,age=200,height=300; 
document.write("box="+box);//输出box=100 
Copy after login

3)、三元操作符:

var box=(3>4)&#63;"对":"错"; 
document.write(box);//输出错 
Copy after login

      如果想更详细的了解ECMAScript运算符的知识,可以访问JavaScript高级教程中的ECMASscript一元运算符这个系列中有详细的运算符教程。对于JS的运算符来说,我们可以对比着C++,C#和Java来学,这个还是相当的容易的。

以上就是关于JavaScript的表达式与运算符的全部内容,希望对大家的学习有所帮助。

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)

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

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.

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

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

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 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

Operator precedence list in Go language, which operator has the highest precedence? Operator precedence list in Go language, which operator has the highest precedence? Jan 03, 2024 pm 04:59 PM

There are many operators in Go language, which are often used to perform various mathematical and logical operations. Each operator has its own precedence, which determines the order in which they are evaluated in an expression. This article will introduce you to the priority ranking of operators in the Go language and find out the operator with the highest priority. The operators in Go language are as follows in order from high to low precedence: parentheses: (). Parentheses are used to change the precedence order of operators. Parentheses in expressions are evaluated first. Unary operators: +, -, !. Unary operator means that only one

See all articles