Different types of operations in JavaScript

王林
Release: 2023-05-09 20:36:06
Original
420 people have browsed it

JavaScript is a widely used programming language used for front-end and back-end development. It contains many different types of operators, which have their unique applications in different situations. This article will introduce in detail the different types of operators in JavaScript and how to use them.

1. Arithmetic operators

Arithmetic operators are the most basic operators for processing numeric type data. They can be used to perform basic arithmetic operations such as addition, subtraction, multiplication, division, etc.

1. Addition operator ( )

The addition operator is used to add two numbers. Additionally, it can also concatenate strings together. For example:

var x = 10; var y = 5; var z = x + y; //输出结果为15
Copy after login

2. Subtraction operator (-)

The subtraction operator is used to subtract two numbers. For example:

var x = 10; var y = 5; var z = x - y; //输出结果为5
Copy after login

3. Multiplication operator (*)

The multiplication operator is used to multiply two numbers. For example:

var x = 10; var y = 5; var z = x * y; //输出结果为50
Copy after login

4. Division operator (/)

The division operator is used to divide one number by another number. For example:

var x = 10; var y = 5; var z = x / y; //输出结果为2
Copy after login

5. Remainder operator (%)

The remainder operator is used to calculate the remainder after dividing two numbers. For example:

var x = 10; var y = 4; var z = x % y; //输出结果为2
Copy after login

2. Comparison operators

Comparison operators are used to compare two values and return a Boolean value (true or false).

1. Equal operator (==)

The equal operator is used to compare whether two values are equal. For example:

var x = 10; var y = "10"; if (x == y) { //执行代码 }
Copy after login

At this time, although the data types of x and y are different, because their values are equal, the condition is judged to be true.

2. Not equal to operator (!=)

The not equal to operator is used to compare whether two values are not equal. For example:

var x = 10; var y = "5"; if (x != y) { //执行代码 }
Copy after login

At this time, since the values of x and y are not equal, the condition is judged to be true.

3. Strict equal operator (===)

The strict equal operator is used to compare whether two values are not only equal, but also have the same data type. For example:

var x = 10; var y = "10"; if (x === y) { //执行代码 }
Copy after login

At this time, because the data types of x and y are different, the condition is judged to be false.

4. Strict inequality operator (!==)

The strict inequality operator is used to compare whether two values are not only not equal, but also have different data types. For example:

var x = 10; var y = "5"; if (x !== y) { //执行代码 }
Copy after login

Since the data types of x and y are different, and their values are not equal, the condition is judged to be true.

5. Greater than operator (>), less than operator (<), greater than or equal to operator (>=) and less than or equal to operator (<=)

these The usage of operators is the same as comparison operators. For example:

var x = 10; var y = 5; if (x > y) { //执行代码 }
Copy after login

This condition is judged to be true because 10 is greater than 5.

3. Logical operators

Logical operators are used to perform logical operations on two or more expressions and return a Boolean value (true or false).

1. Logical AND operator (&&)

The logical AND operator is used to perform logical AND (AND) operations on two expressions. For example:

var x = 10; var y = 5; if (x > 5 && y < 10) { //执行代码 }
Copy after login

Since x is greater than 5 and y is less than 10, the condition is judged to be true.

2. Logical OR operator (||)

The logical OR operator is used to logically OR (or) operate two expressions. For example:

var x = 10; var y = 5; if (x > 5 || y > 10) { //执行代码 }
Copy after login

Because x is greater than 5 or y is greater than 10, the condition is judged to be true.

3. Logical NOT operator (!)

The logical NOT operator is used to negate an expression. For example:

var x = 10; var y = 5; if (!(x > y)) { //执行代码 }
Copy after login

At this time, the condition that x is greater than y is judged to be true, but due to the addition of the inverse logical NOT operator, the final condition is judged to be false.

4. Bit operators

Bit operators perform operations on the binary representation of values. They operate on each bit in the binary representation of the number separately.

1. Bitwise AND operator (&)

The bitwise AND operator performs an AND operation on the binary values of two numbers. For example:

var x = 5 & 1; //输出结果为1
Copy after login

2. Bitwise OR operator (|)

The bitwise OR operator performs an OR operation on the binary values of two numbers. For example:

var x = 5 | 1; //输出结果为5
Copy after login

3. Bitwise XOR operator (^)

The bitwise XOR operator performs an XOR operation on the binary values of two numbers. For example:

var x = 5 ^ 1; //输出结果为4
Copy after login

4. Negation operator (~)

The negation operator performs the inversion operation on the binary value of a number. For example:

var x = ~5; //输出结果为-6
Copy after login

5. Left shift operator (<<)

The left shift operator moves the binary representation of a number to the left by the specified number of digits. For example:

var x = 5 << 2; //输出结果为20
Copy after login

6. Right shift operator (>>)

The right shift operator moves the binary representation of a number to the right by the specified number of digits. For example:

var x = 5 >> 2; //输出结果为1
Copy after login

The above are the different types of operators in JavaScript and how to use them. Understanding the application of these operators allows developers to write better code and improve code execution efficiency.

The above is the detailed content of Different types of operations in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!